Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Saturday, October 26, 2019

Online VB Compiler - online editor

Here is a great Visual Basic editor online. You can test and run code here. The site also supports other languages!

Online VB Compiler - online editor: OnlineGDB is online IDE with VB compiler. Quick and easy way to run VB program online.

Thursday, September 30, 2010

How to read the beginning of a really big file - CodeProject

for vbsImage via Wikipedia

Introduction

I had a problem reading a header of a very large file in Notepad. It took forever do download 300 MB file from a file share. Out of frustration I wrote this very small VBS script that will read 10 lines of the file and show it in a message box. I hope someone will find it useful.
How to read the beginning of a really big file - CodeProject
Enhanced by Zemanta

Saturday, July 10, 2010

VB 9.0, C# 3.0 API Viewer - CodeProject

Have you been looking for an API viewer for VB 9 and C# 3? Has the old API viewers not been able to work in Vista or Windows 7? VSAPIVIEWER is a remake of Pramod Kumar Singh's 2002 Api Viewer. Pramod's version, I believe, was written in VB.net 7.0.
As I was searching for an API viewer, I came across a lot of questions in the MSDN forums that were asking if anyone knew of a site where they could get a current api viewer for VS 2008 and the answer was always "NO". I beleive that this is a well needed utility.



VB 9.0, C# 3.0 API Viewer - CodeProject
Enhanced by Zemanta

Wednesday, May 19, 2010

Converting Vb.Net to C#

Here are some  tools for converting VB.Net to C#:

developerFusion is an online application that allows you to enter code  blocks  in VB.Net or C# and convert it. VB  to C# and vice versa.


DOTNET Spider gives you the same functionality.





VB.Net to C# Converter 2.2 - is used to convert VB.Net projects to C#










C-Sharpener for VB is for converting VB.Net projects to C#


Reblog this post [with Zemanta]

Saturday, October 17, 2009

Python and ODBC

Microsoft Office AccessImage via Wikipedia
For anyone following this blog, you know that I've been learning Python. I've been researching the method of writing scripts for reading and interacting with relational databases in general and with Microsoft Access in particular. I decided to try out a project in both Python and VB.Net.  The project is actually really simple. Given two databases, compare the table from one of them with the other to see if there is missing data. In my example I have a reference database containing the list of  tree species to compare to a table of inventory tree information. The applications however are generalized. You can use them with any two Access Databases.

Given the paths of each database, the names of each tables, the fields for comparison, and the path for the final output file, each application will work return a list of species found in the main table that are not in the reference table to the screen and to the output file. I'm going to post each program, a link to download it.

Here are my inputs that I am using:

Reference Database: Personal_FSVEG.mdb
Reference Table: fs_nris_fsveg_NRV_R5_SPECIES_XWALK
Reference Field: NRIS_SPEC

InputDatase: mnfplus07_1inv.mdb
Input Table: FSVEG_TXPT_V
Input Field: SPEC

Output Files
Missing_Species_VB.txt
Missing_Species_Python_adodb.txt
Missing_Species_Python_Win32com.txt


Accessing the database in VB.Net through ADO.Net

It did not take me long to write this but, because it so easy I gave it a GUI.

Here is the code
Imports System.IO
Public Class frmMainForm
    Dim whatfile, var, whatpath, pathvar As String
    Dim U As Integer
    Dim DS As DataSet
    Dim projectfile As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmMain.Click

        Dim var As Object, i As Integer, ConnectionString As String, q As String
        Dim adapter As New OleDb.OleDbDataAdapter

        Me.OpenFileDialog1.InitialDirectory = "C:\"
        Me.OpenFileDialog1.Filter = "Access Database (*.mdb)|*.MDB"
        Me.OpenFileDialog1.ShowDialog()
        whatfile = Me.OpenFileDialog1.FileName
        If whatfile <> vbNullString Then

            var = Split(whatfile, "\", -1)
            U = 0
            For Each q In var
                U = U + 1
            Next q

            For i = 0 To U - 2
                whatpath = whatpath & var(i) & "\"
            Next i

        End If

        Me.txtinputfile.Text = whatfile
        pathvar = whatpath

        DS = New Data.DataSet
        DS.Clear()
        ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtinputfile.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)
        conn.Open()
        Dim dt As Data.DataTable = conn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
                      New Object() {Nothing, Nothing, Nothing, "TABLE"})
        'List the table name from each row in the schema table.
        For i = 0 To dt.Rows.Count - 1
            Me.cboCompareTable.Items.Add(dt.Rows(i)!TABLE_NAME.ToString)
        Next i

        'Explicitly close - don't wait on garbage collection.
        conn.Close()

    End Sub

    Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click

        Dim adapterTXPT As New OleDb.OleDbDataAdapter
        Dim ConnectionString2 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtinputfile.Text

        Dim adapter As New OleDb.OleDbDataAdapter
        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtReferenceFile.Text

        adapterTXPT = New OleDb.OleDbDataAdapter("SELECT DISTINCT " & Me.cboCompaeField.Text & " FROM " & Me.cboCompareTable.Text, ConnectionString2)
        ' Code to modify data in DataSet here

        Try
            adapterTXPT.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapterTXPT.Fill(DS, "CompareTable")
        Catch
            MsgBox("Error is: " & Err.Number & " " & Err.Description)
        End Try

        ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtReferenceFile.Text
        adapter = New OleDb.OleDbDataAdapter("SELECT DISTINCT " & Me.cboReferenceField.Text & " FROM " & Me.cboReferenceTable.Text, ConnectionString)
        ' Code to modify data in DataSet here

        Try
            adapterTXPT.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapterTXPT.Fill(DS, "CompareTable")
        Catch
            MsgBox("Error is: " & Err.Number & " " & Err.Description)
        End Try

        Try
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapter.Fill(DS, "ReferenceTable")
        Catch
            MsgBox("Error is: " & Err.Number & " " & Err.Description)
        End Try

        If File.Exists(projectfile) Then
            File.Delete(projectfile)
        End If

        Dim writenow As Boolean = True
        Dim MyFile As IO.StreamWriter
        MyFile = New System.IO.StreamWriter(projectfile, True) ' True for appending

        MyFile.WriteLine("Missing Species found in the TXPT")

        Dim dtTXPT As DataTable
        dtTXPT = DS.Tables("CompareTable")
        Dim dtREF As DataTable
        dtREF = DS.Tables("ReferenceTable")

        Dim dr As DataRow, SPEC As String, drref As DataRow
        writenow = True
        For Each dr In dtTXPT.Rows
            SPEC = dr(Me.cboCompaeField.Text)
            For Each drref In dtREF.Rows
                If SPEC = drref(Me.cboReferenceField.Text) Then
                    writenow = False
                    Exit For
                End If
            Next

            If writenow Then
                lstTXPT.Items.Add(SPEC)
                MyFile.WriteLine(SPEC) ' Write text to file

            End If

            writenow = True

        Next

        MyFile.Close() ' Close file 
        MsgBox("Done")

    End Sub

    Private Sub cmdQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuit.Click
        End
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Me.FolderBrowserDialog1.Reset()
        Me.FolderBrowserDialog1.ShowDialog()
        pathvar = Me.FolderBrowserDialog1.SelectedPath.ToString
        Dim projectname As String = InputBox("Type the name you want to give the report file:", "Choose name for Report")
        projectfile = pathvar & "\" & projectname
        Me.txtoutput.Text = projectfile
    End Sub

    Private Sub frmMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub cmdBrowseRefernceFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseRefernceFile.Click
        Dim var As Object, i As Integer, ConnectionString As String, q As String
        Dim adapter As New OleDb.OleDbDataAdapter
        Dim adapterTXPT As New OleDb.OleDbDataAdapter
        Dim adapterVXPT As New OleDb.OleDbDataAdapter

        Me.OpenFileDialog1.InitialDirectory = "C:\"
        Me.OpenFileDialog1.Filter = "Access Database (*.mdb)|*.MDB"
        Me.OpenFileDialog1.ShowDialog()
        whatfile = Me.OpenFileDialog1.FileName
        If whatfile <> vbNullString Then

            var = Split(whatfile, "\", -1)
            U = 0
            For Each q In var
                U = U + 1
            Next q

            For i = 0 To U - 2
                whatpath = whatpath & var(i) & "\"
            Next i

        End If

        Me.txtReferenceFile.Text = whatfile
        pathvar = whatpath

        DS = New Data.DataSet
        DS.Clear()
        ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtReferenceFile.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)
        conn.Open()
        Dim dt As Data.DataTable = conn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
                      New Object() {Nothing, Nothing, Nothing, "TABLE"})
        'List the table name from each row in the schema table.
        For i = 0 To dt.Rows.Count - 1
            Me.cboReferenceTable.Items.Add(dt.Rows(i)!TABLE_NAME.ToString)
        Next i

        'Explicitly close - don't wait on garbage collection.
        conn.Close()
    End Sub

    Private Sub cboCompareTable_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCompareTable.SelectedValueChanged

        Dim Connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtinputfile.Text
        Dim adapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & Me.cboCompareTable.Text, Connectionstring)

        Try
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapter.Fill(DS, Me.cboCompareTable.Text)
        Catch
            MsgBox("Error is: " & Err.Number & " " & Err.Description)
        End Try

        Dim fld As DataColumn, dr As DataRow
        dr = DS.Tables(Me.cboCompareTable.Text).Rows(0)
        For Each fld In dr.Table.Columns
            Me.cboCompaeField.Items.Add(fld.ColumnName)
        Next
    End Sub

    Private Sub cboReferenceTable_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboReferenceTable.SelectedValueChanged

        Dim Connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.txtReferenceFile.Text
        Dim adapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & Me.cboReferenceTable.Text, Connectionstring)

        Try
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapter.Fill(DS, Me.cboReferenceTable.Text)
        Catch
            MsgBox("Error is: " & Err.Number & " " & Err.Description)
        End Try

        Dim fld As DataColumn, dr As DataRow
        dr = DS.Tables(Me.cboReferenceTable.Text).Rows(0)
        For Each fld In dr.Table.Columns
            Me.cboReferenceField.Items.Add(fld.ColumnName)
        Next

    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        With Me
            .txtinputfile.Text = ""
            .txtoutput.Text = ""
            .txtReferenceFile.Text = ""

            Dim t As Integer, i As Integer
            i = .lstTXPT.Items.Count
            For t = 1 To i
                .lstTXPT.Items.RemoveAt(0)
            Next

            i = .cboCompareTable.Items.Count
            For t = 1 To i
                .cboCompareTable.Items.RemoveAt(0)
            Next

            i = .cboReferenceField.Items.Count
            For t = 1 To i
                .cboReferenceField.Items.RemoveAt(0)
            Next

            i = .cboReferenceTable.Items.Count
            For t = 1 To i
                .cboReferenceTable.Items.RemoveAt(0)
            Next

            i = .cboCompaeField.Items.Count
            For t = 1 To i
                .cboCompaeField.Items.RemoveAt(0)
            Next

        End With
    End Sub
End Class


Here is the VB.Net version in action






I like doing this kind of work in VB because GUIs are so easy and fast to put together.


Accessing the database in Python

Fortunately, there was plenty of information about how to achieve the same effect in Python. I found two ways of doing it. Neither way offers a GUI but instead a user interactively enters inputs using commandline.

Accessing the database in Python through ADOdb/mxODBC

In scouring the Internet, I found the following library for Python. ADOdb depends on the mxODBC library. The information for how to get them can be found here and there. As I stated in a previous post, the mxODBC library is not free but you can get a 30-day license in which to to test. It is also important to remember that you must install these libraries to get the following script to work.

Here is the Python code using the ADOdb/mxODBC libraries
'''
Created on Oct 13, 2009

@author: marcusmcelhaney
'''
#This script will be designed to compare a given inventory file versus a species reference table
#This is to test ODBC connection to Access Databases
#Results will be printed out on screen and in a file.
#Uses Python 2.5

#Used the technique from http://phplens.com/lens/adodb/adodb-py-docs.htm#install


#Start connections to Access database
# ODBC for Access database containing the Reference table
import adodb
import os.path

lstTXPT = []

#get the location of the reference file
reffile = input('Enter the file path and name for reference file: ')
inputfile2 = os.path.split(reffile)
reffile  = inputfile2[0] + "\\" + inputfile2[1]
#reffile = "C:/Temp/Personal_FSVEG.mdb"

#get Reference SQL Statement
reffield = input("Enter field to get Records from the reference table: ")
#reffield="NRIS_SPEC"

#Get Reference table
refTable = input("Enter Reference Table: ")
#refTable = "fs_nris_fsveg_NRV_R5_SPECIES_XWALK"

conn0 = adodb.NewADOConnection('access') # mxodbc required
dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" + reffile + ";"
conn0.Connect(dsn)
cursor = conn0.Execute('select distinct ' + reffield + " from " + refTable)


#get the location of the inventory file
inputfile = input('Enter the file path and name for inventory: ')
inputfile2 = os.path.split(inputfile)
inputfile  = inputfile2[0] + "\\" + inputfile2[1]

#inputfile = inputfile.replace("\", "/",5)

#Get compare field
comparefield = input("Enter the field to compare in the desired table: ")
#comparefield = "SPEC"

#Get compare table
CompareTable = input("Enter the name of the table you are comparing: ")
#CompareTable = "FSVEG_TXPT_V"

out_file = open("c:/Temp/Missing_Species_Python_adodb.txt", "w")

#Make connection string for inventory file
conn2 = adodb.NewADOConnection('access') # mxodbc required
dsn2 = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" + inputfile + ";" 
conn2.Connect(dsn2)
cursorTXPT = conn2.Execute('select distinct ' + comparefield + ' from ' + CompareTable)

print "Getting list of comparefield not in Compare Table"
writenow = True
cursorTXPT.MoveNext()
while not cursorTXPT.EOF:
    DesiredSPEC = cursorTXPT.fields[0]
    
    while not cursor.EOF:
        if DesiredSPEC == cursor.fields[0]:
            writenow = False
            cursor = conn0.Execute('select distinct ' + reffield + " from " + refTable)
            break
        cursor.MoveNext()
      
    if writenow:       
        lstTXPT.append(DesiredSPEC)
        out_file.write(DesiredSPEC)
        cursor = conn0.Execute('select distinct ' + reffield + " from " + refTable)
        
    writenow = True
    cursorTXPT.MoveNext()


cursorTXPT.Close()    
cursor.Close()
conn2.Close()
conn0.Close() 

print "Missing Species from the TXPT"
for species in lstTXPT:
    print species
    
out_file.close()
print "Done"  

Accessing the database in Python through Win32com

Here is the other method I found for connecting a python script into an Access database. I think this will be my method of choice because it's library is free and the syntax and use closely mirrors that of ADO classic which I am very familiar with. Here is the code:

'''
Created on Oct 15, 2009

@author: marcusmcelhaney
'''
#This script will be designed to compare a given inventory file versus a species reference table
#This is to test ODBC connection to Access Databases
#Results will be printed out on screen and in a file.
#Uses Python 2.5

#Used the technique Win32com


#Start connections to Access database
# ODBC for Access database containing the Reference table
import win32com.client                        
import os.path

lstTXPT = []

#get the location of the reference file
reffile = input('Enter the file path and name for reference file: ')
inputfile2 = os.path.split(reffile)
reffile  = inputfile2[0] + "\\" + inputfile2[1]
#reffile = "C:/Temp/Personal_FSVEG.mdb"

#get Reference SQL Statement
reffield = input("Enter field to get Records from the reference table: ")
#reffield="NRIS_SPEC"

#Get Reference table
refTable = input("Enter Reference Table: ")
#refTable = "fs_nris_fsveg_NRV_R5_SPECIES_XWALK"

connexion = win32com.client.gencache.EnsureDispatch('ADODB.Connection')            
dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" + reffile + ";"
connexion.Open(dsn)

AD_OPEN_KEYSET = 1
AD_LOCK_OPTIMISTIC = 3

rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs.CursorLocation = 3 

offset = 1            
    
rs.Open('select distinct ' + reffield + " from " + refTable, connexion, AD_OPEN_KEYSET, AD_LOCK_OPTIMISTIC)

#get the location of the inventory file
inputfile = input('Enter the file path and name for inventory: ')
inputfile2 = os.path.split(inputfile)
inputfile  = inputfile2[0] + "\\" + inputfile2[1]

#inputfile = inputfile.replace("\", "/",5)

#Get compare field
#comparefield = input("Enter the field to compare in the desired table: )
comparefield = "SPEC"

#Get compare table
CompareTable = input("Enter the name of the table you are comparing: ")
#CompareTable = "FSVEG_TXPT_V"

out_file = open("c:/Temp/Missing_Species_Python_win32com.txt", "w")

#Make connection string for inventory file
connexion2 = win32com.client.gencache.EnsureDispatch('ADODB.Connection')            
dsn2 = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" + inputfile + ";" 
connexion2.Open(dsn2)

AD_OPEN_KEYSET = 1
AD_LOCK_OPTIMISTIC = 3

rsTXPT = win32com.client.Dispatch(r'ADODB.Recordset')
rsTXPT.CursorLocation = 3 

offset = 1            
    
rsTXPT.Open('select distinct ' + comparefield + ' from ' + CompareTable, connexion2, AD_OPEN_KEYSET, AD_LOCK_OPTIMISTIC)

print "Getting list of comparefield not in Compare Table"
writenow = True

# expand the recordset as list of tuples
while not rsTXPT.EOF: 
    rsTXPT.MoveNext
    DesiredSPEC = rsTXPT.Fields.Item(comparefield).Value
    rs.MoveNext
    while not rs.EOF:
        
        if DesiredSPEC == rs.Fields.Item(reffield).Value:
            writenow = False
            rs.Close()
            rs.Open('select distinct ' + reffield + " from " + refTable, connexion, AD_OPEN_KEYSET, AD_LOCK_OPTIMISTIC)
            break
        rs.MoveNext()
      
    if writenow:       
        lstTXPT.append(DesiredSPEC)
        out_file.write(DesiredSPEC)
        rs.Close()
        rs.Open('select distinct ' + reffield + " from " + refTable, connexion, AD_OPEN_KEYSET, AD_LOCK_OPTIMISTIC)
        
    writenow = True
    rsTXPT.MoveNext()


rsTXPT.Close()    
rs.Close()
connexion2.Close()
connexion.Close() 

print "Missing Species from the TXPT"
for species in lstTXPT:
    print species
    
out_file.close()
print "Done" 

You can down load the source code below


Download these
Name
Purpose
Link
VB.Net version
Software installer
Setup1
Setup1.msi
Hosted by eSnips
VB.Net Source code
Project files
SpeciesMatch
SpeciesMatch.zip
Hosted by eSnips
Python ADOdb Source code
Project files
TestSpeciesCode_adodb
TestSpeciesCode_ad...
Hosted by eSnips
Python Win32com Source code
Project files
TestSpeciesCode_Win32Com
TestSpeciesCode_Wi...
Hosted by eSnips
Personal_FSVEG.mdb
Reference database
Personal_FSVEG
Personal_FSVEG.mdb
Hosted by eSnips
Comparing Inventory
Test database
Personal_FSVEG
Personal_FSVEG.mdb
Hosted by eSnips

Thursday, October 15, 2009

Heisencoder: Adding Syntax Highlighting to Blogger


A method for adding code from several languages in blogger, including Python!

Heisencoder: Adding Syntax Highlighting to Blogger

Shared via AddThis

Post HTML, C#, VB.net code with formating : saAction


 Here is a great post giving 5 methods for posting programming code in Blogger post. Works for  a variety of languages.

Post HTML, C#, VB.net code with formating : saAction

Shared via AddThis

Friday, October 2, 2009

Visual Basic to Python Converter


I found a program called vb2Py, which converts Visual Basic code to Python. You can download a desktop application or paste your VB code into a text box to see what the Python equivalent is. This is definitely a web site to keep an eye on.

Visual Basic to Python Converter

Thursday, July 2, 2009

Microsoft Has A...Blog?


I was doing some research for my job today and I found out several things I didn't know. I was searching for information on Microsoft Visual Studio 2008. VS 2008 is an Integrated Developer Environment (IDE) which just means that it's software to help write other software. I have used previous versions of it practically daily since 1999. So Now we are looking to upgrade to a newer version. I've got to admit that I started with version 6 (released in 1998) and the product has only improved over the past 10 years (dang...am I that old?). I cam to find out (shock) that Microsoft is looking to release version 2010 next year and a Beta version is already available for download and (awe!) there is maintaining a blog on Google's blogspot for development notes and news on programming with Microsoft Products!

After comparing 2008 specs with 2010 specs it is my opinion that i should just upgrade to 2010 when it comes out and skip 2008 altogether. We want to be able to access the .Net coding languages, Python, Silverlight, parallel processing on multiple CPUs, and multi-threading. Visual Studio 2010 will give us all of this including easier automation of Microsoft Office products. Here is another site for more info on VS 2010 features.

The other thing is I found out about a coding library that can be run on Visual Studio 2005 or newer for making Facebook applications on top of ASP.Net application. It's called Facebook.Net and I've got to download it and check it out.

Here are the links I found on it.
Facebook.Net
VS.NET starter kit for Nikhil Kothari's Facebook.NET
Nikkil's blog post