| Add caption |
Create a Visual Basic Form Design
1) Add ADODC component and bulid connection to "Test.mdb"
2) Add datagrid and Set Property DataSource =ADODC
3) Add Command Button
Write following Code on Command1 click
Private Sub Command1_Click()
'Define the three objects that we need,
' A Connection Object - connects to our data source
' A Command Object - defines what data to get from the data source
' A RecordSet Object - stores the data we get from our data source
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim res As New ADODB.Recordset
'Defines the connection string for the Connection. Here we have used fields
'Provider, Data Source and Mode to assign values to the properties
' conConnection.Provider and conConnection.Mode
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\" & "test.mdb;Mode=Read|Write"
'Define the location of the cursor engine, in this case we are opening an Access database
'and adUseClient is our only choice.
con.CursorLocation = adUseClient
'Opens our connection using the password "Admin" to access the database. If there was no password
'protection on the database this field could be left out.
con.Open
'Defines our command object
' .ActiveConnection tells the command to use our newly created command object.
' .CommandText tells the command how to get the data, in this case the command
' will evaluate the text as an SQL string and we will return all
' records from a table called tabTestTable
' .CommandType tells the command to evaluate the .CommandText property as an SQL string.
With cmd
.ActiveConnection = con
.CommandText = "SELECT * FROM Table1;"
.CommandType = adCmdText
End With
'Defines our RecordSet object.
' .CursorType sets a static cursor, the only choice for a client side cursor
' .CursorLocation sets a client side cursor, the only choice for an Access database
' .LockType sets an optimistic lock type
' .Open executes the cmdCommand object against the data source and stores the
' returned records in our RecordSet object.
With res
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmd
End With
'Firstly test to see if any records have been returned, if some have been returned then
'the .EOF property of the RecordSet will be false, if none have been returned then the
'property will be true.
If res.EOF = False Then
'Move to the first record
res.MoveFirst
'Lets move through the records one at a time until we reach the last record
'and print out the values of each field
Do
'Access the field values using the fields collection and print them to a message box.
'In this case I do not know what you might call the columns in your database so this
'is the safest way to do it. If I did know the names of the columns in your table
'and they were called "Column1" and "Column2" I could reference their values using:
' res!Column1
' res!Column2
MsgBox "Record " & res.AbsolutePosition & " " & _
res.Fields(0).Name & "=" & res.Fields(0) & " " & _
res.Fields(1).Name & "=" & res.Fields(1)
'Move to the next record
res.MoveNext
Loop Until res.EOF = True
'Add a new record
With res
.AddNew
.Fields(1) = "New"
.Fields(2) = "Record"
.Update
End With
'Move back to the first record and delete it
res.MoveFirst
'To Delete Record
'res.Delete
res.Update
'Close the recordset
res.Close
Else
MsgBox "No records were returned using the query " & cmd.CommandText
End If
'Close the connection
con.Close
'Release your variable references
Set con = Nothing
Set cmd = Nothing
Set res = Nothing
End Sub
All the Best...
No comments:
Post a Comment