Promote Your Blog

Saturday, January 28, 2012

How To Add Data To Tables in MS Access Database Using ADODB and Visual Basic

We have already created database in my post How To Create MS Access Database Using ADOX and Visual Basic and learnt how to add table to database in post How To Add Tables to MS Access Database Using ADOX and Visual Basic. Now its time to add data to the added table in created database. Here is the code to do this using ADODB:

Imports ADODB

Const adOpenStatic = 3
Const adLockOptimistic = 3

Dim objConn As ADODB.Connection
Dim objRS
Dim strDBName, strTable As String
strDBName = "MyLibrary.mdb"
strTable = "Books"
objConn = CreateObject("ADODB.Connection")
objRS = CreateObject("ADODB.Recordset")
objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDBName)
objRS.Open("SELECT * FROM " & strTable,
objConn, adOpenStatic, adLockOptimistic)

objRS("Book") = tbBook.Text
objRS("Author") = tbAuthor.Text


objRS = Nothing
objConn = Nothing
MsgBox("Data Added Successfully")

Book and Author names are read from the GUI. Note that connection and recordset are closed before setting to Nothing

Getting confused first ADOX and then ADODB...Let me add more to confusion by adding more terms like ADO, ADO.NET, DAO, RDO, OLEDB, ODBC...

To sort out this confusion, read post on Microsoft Data Access Components at blog on Computer Science Notes.

How To Add Tables To An MS Access Database Using ADOX and Visual Basic

Let us add table to database created in my post How To Create MS Access Database Using ADOX and Visual Basic

Dim ADOXcatalog As New ADOX.Catalog
Dim ADOXtable As New ADOX.Table
Dim ADOXindex As New ADOX.Index

On Error GoTo errhandler
ADOXcatalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "MyLibrary.mdb")

On Error Resume Next

ADOXcatalog.ActiveConnection = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& "MyLibrary.mdb"

'name table, append fields to table
ADOXtable.Name = "Books"

'ADOXtable.Columns.Append("ID", ADOX.DataTypeEnum.adInteger)
ADOXtable.Columns.Append("Book", ADOX.DataTypeEnum.adVarWChar, 200)
ADOXtable.Columns.Append("Author", ADOX.DataTypeEnum.adVarWChar, 30)

'append tables to database

'internal index on two fields
'ADOXindex.Name = "TwoColumnsIndex" 'name of index


If Err.Number = -2147217897 Then
MsgBox("Database already exists")
ElseIf Err.Number <> 0 Then
MsgBox("Err " & Err.Description & "; operation not complete")
End If
ADOXtable = Nothing
ADOXindex = Nothing
ADOXcatalog = Nothing

Since database already exists, error handler will be invoked and this message will be prompted. Cleaning is very important by setting to Nothing

How To Create An MS-Access Database Using ADOX and Visual Basic

Dim acat As New ADOX.Catalog()

Dim sCreateString As String
sCreateString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
"MyLibrary.mdb" 'you can give path also otherwise db will created in app folder

Catch Excep As System.Runtime.InteropServices.COMException
MsgBox("DB Cannot be created")
cat = Nothing 'clean up is very important
End Try

Monday, January 23, 2012

Operations on DataGridView In Visual Basic - Part 1

1. Adding a Row to DataGridView Programmatically,

Dim col1content,col2content As String
Dim col2content As Integer
2. Clearing contents of all the rows,


3. To read all cells row by row,

For i = 0 To myDataGridView.Rows.Count - 2
' -2 bcoz we are starting with i=0 and last row is empty row added
col1 = myDataGridView.Row(i).Cells(0).Value.ToString()
col2 = myDataGridView.Row(i).Cells(1).Value.ToString()
col3 = myDataGridView.Row(i).Cells(3).Value 'Integer Value

4. Reading a column of selected row,

Dim s As String = myDataGridView .CurrentRow.Cells("ColumnName").Value

5. To change color of text of selected row,

Dim low_score_style As New DataGridViewCellStyle()
low_score_style.ForeColor = Color.Red

myDataGridView .CurrentRow.DefaultCellStyle = low_score_style

6. To change colors of all rows iteratively,
For i = 0 To myDataGridView.Rows.Count - 2
myDataGridView.Rows(i).DefaultCellStyle = low_score_style

7. Alternate way of achieving 6,
myDataGridView.ForeColor = Color.Red

To come up with some of the above tips on DataGridView, I had dived deep into Internet for days to make your life easier ...

How To Kill Process With Given Name In Visual Basic

Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("myprocess")
For Each p As Process In pProcess

Accessing Processes In Visual Basic

To access processes with given name and setting their affinity,

Dim pa As System.IntPtr = 'integer

Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("myprocesses")

For Each pr As Process In pProcess
pr.ProcessorAffinity = pa

1. Remove .exe (or any other executable extension) from process name
2. To get all processes on given machine,
Dim pProcess() As Process = System.Diagnostics.Process.GetProcesses()

Not giving any machine name in above statement means localhost

How To Create Process on Remote Machine In Visual Basic

Dim oProcess As Object
Dim lProcessID As Long
Dim lRet As Long
Dim runstr As String
Dim RemoteHostName, ProcessName As String

RemoteHostName = "MyServer"

ProcessName = "mspaint.exe"

runstr = "winmgmts:\\" & RemoteHostName & "\root\cimv2:Win32_Process"

oProcess = GetObject(runstr)
lRet = oProcess.Create(ProcessName, , ,IProcessID)
Catch errorVariable As Exception
End Try

There are certain issues like WMI and RPC must be enabled on remote machine and you must login with administrative rights etc.

Exception Handling In Visual Basic


'Your piece of code

Catch errorVariable As Exception


End Try

How To Read From A File In Visual Basic

Imports System.IO

Dim readLineByLine As String

Dim fileReader As New StreamReader("fileToBeRead.xtn")

Do Until fileReader.EndOfStream
readLineByLine = fileReader.ReadLine()
'Take some action on line read


How To Write To A File In Visual Basic

Imports System.IO

Dim ValueOfVariable As String = "Hello, World!"

Dim streamOut As New StreamWriter("filename.extension")

streamOut.WriteLine("Your First Line Hard-Coded")



How To Use Command Line Arguments In Visual Basic

1. To get number of command line arguments:
Dim argc As Integer = Environment.GetCommandLineArgs().Length

2. To retrieve all command line arguments as array of strings:
Dim args() As String = Environment.GetCommandLineArgs()

3. To access a particular command line argument:
Dim progName As String = args(0).ToString

Remember, first argument i.e. args(0) is program name itself
==> Minimum value of argc is 1

Saturday, January 21, 2012

How To Generate Indian Map Through C Program

#include "stdio.h"

int a,b,c;
int count = 1;

for (b = c = 10;
= "- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!"[b+++21]; )
for(; a-- > 64 ; )
( ++c=='Z' ?
= c / 9 :
33 ^ b & 1 );

return 0;

Thursday, January 19, 2012

How To Resize Window to Screen Size in VC++

Add following code to OnCreate function in MainFrm.cpp:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)


CClientDC mDC(this) ;

int x = mDC.GetDeviceCaps(HORZRES);

int y = mDC.GetDeviceCaps(VERTRES);