View Full Version : Run the code - nothing happens :(


ppataki
12-11-2008, 12:32 AM
Dear All,

If I run the below code simply nothing happens, did I do something wrong?


Private Sub ListValues()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(Name:="companies", Type:=dbOpenSnapshot)
Do While Not rst.EOF
Debug.Print "Company ID: " & rst![ID] _
& vbCrLf & vbTab & "Company name: " & rst![Companyname] & vbCrLf
rst.MoveNext
Loop
End Sub



Many thanks in advance! ;)

MStef
12-11-2008, 12:41 AM
Hello ppataki!
It is not clear what do you exactly want.
Send a looks of your table (with some records), and tell what do you want.

ppataki
12-11-2008, 12:49 AM
Hello,

I am just playing around with VBA to get more acquainted with it
I expect from this code to display the records of the table for me
I just have two fields in the table, ID and CompanyName

Many thanks!

chergh
12-11-2008, 12:55 AM
add in the lines


rst.movelast
debug.print rst.recordcount


Might be there are no records in your recordset

namliam
12-11-2008, 01:02 AM
Make sure you have the debug window open (CTRL+G)

ppataki
12-11-2008, 01:06 AM
Thank you namliam!!
Now it works perfectly!

namliam
12-11-2008, 01:14 AM
FYI
Debug.Print "Company ID: " & rst![ID] _
& vbCrLf & vbTab & "Company name: " & rst![Companyname] & vbCrLf
I dont think the last return is required, Print adds an enter to the end of the line anyway.

Alternative, messagebox:

Private Sub ListValues()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
dim strText as string
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(Name:="companies", Type:=dbOpenSnapshot)
strString = ""
Do While Not rst.EOF
strString = strString & "Company ID: " & rst![ID] & vbCrLf & _
vbTab & "Company name: " & rst![Companyname] & vbCrLf
rst.MoveNext
Loop
msgbox strString
End Sub

And please please PLEASE DO indent your code.... this makes it much easier to find how/when/where loop/if/sub/functions start and end.... Makes maintaining your DB/code much easier to do.

Also please FORMAT it to look like you intend the text to look, for both please see my code above

MStef
12-11-2008, 01:21 AM
I don't understand why do you use VBA for this.
You can do that via SELECT QUERY.

namliam
12-11-2008, 01:23 AM
I am just playing around with VBA to get more acquainted with it


Thats why.....