Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed to open a table using ado

Status
Not open for further replies.

Juddy58

Technical User
Jan 21, 2003
176
AU
Hello i need to use ado to open a table and add data to it, within a form load event. i am having difficulties in the opening of the table.
The following code i have is

Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cn = CurrentProject.Connection
rst = cn.Open("table")

This is driving me crazy i have tried many different variations of this code but still get different types of error messages like:
expected function or variable!

All errors seem to be occuring in the final line of the shown code. Im sure its some stupid thing i have forgotten as ado is new to me.
Any advice would be greatly appreciated thanks
 
'here's a template I use often
'A quick way to get the SQL statement:
'create a saved query, and paste the SQL view
'for the cmdSQL.CommandText

Dim cnn1 As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmdSQL As ADODB.Command
Dim grArray() As Variant
Dim grCount As Integer
Dim int1 As Integer
Dim int2 As Integer

' Open connection.
Set cnn1 = CurrentProject.Connection
Set cmdSQL = New ADODB.Command

With cmdSQL

.CommandText = "SELECT ...FROM .... WHERE .... "
.CommandType = adCmdText

End With

'Debug.Print cmdSQL.CommandText

Set rst = cmdSQL.Execute()

Do While Not rst.EOF
grArray = rst.GetRows
Loop

grCount = rst.Fields.Count
'Debug.Print "rst.Fields.Count= " & grCount

rst.Close
Set rst = Nothing

Set cmdSQL = Nothing

If IsArray(grArray) Then
'For int2 = 0 To UBound(grArray, 2)
' For int1 = 0 To UBound(grArray, 1)
' Debug.Print "index " & int1 & " value " & grArray(int1, int2)
' Next int1
'Next int2
 
Not sure why the previous example is using an array. The recordset can be disconnected. Anyway that is a different question. To answer the original try this...

Dim cnn as new adodb.connection
Set cnn = currentproject.connection

Dim lrs as new adodb.recordset
lrs.open "Table_Name",cnn,adOpenForwardOnly,
adLockReadOnly, -1

Do While Not lrs.eof
'Action
Loop

lrs.close
cnn.close
 
Oops...Forgot move next...loop should look like this...

Do While not lrs.eof
'Action
lrs.movenext
Loop
 
Thanks alot for your help fellas. i think i will do the same in regards to saving these as templates!
Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top