borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Please Help- Need simple VB code (similar to Macro))Next Topic (HELP on exporting data from excel to word) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Connection String and SQL to retrieve records Solved Topic
Poster Message
VbPanicStation
Level: Big Cheese

Registered: 13-12-2004
Posts: 21

icon Connection String and SQL to retrieve records

Hi,
    Please help!!! I have not used ado connection strings and SQL statements before so I am really stuck.
What I am trying to do is access some tables/ query within the same ACCESS 2003/ 2000 database using VBA and then use the data to send out an 'automatic' email.

Questions:
1. How do I set up a connection string in VBA such that I can access the data in the tables within the same ACCESS database?

2. How do I write and run the SQL statements to retrieve the data?

PLEASE HELP!!

11-04-2006 at 08:39 AM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Connection String and SQL to retrieve records

Hi,
You don't need to create a connection for data in the same Access Database, you use the connection that Access has already created - you get at it through the  "CodeProject.Connection" Property.
So just declare your ADO Recordset, create the SQL String for the data that you want, and then open the Recordset.

Dim rst As ADODB.Recordset
Dim strSQL As String
Set rst = New ADODB.Recordset

'To only get a selection of data
strSQL = "SELECT TableName.* FROM TableName WHERE ((TableName.IDField)=26)"
rst.Open strSQL, CodeProject.Connection, adOpenDynamic, adLockOptimistic, adCmdText

'To get a whole table
rst.Open "TableName", CodeProject.Connection, adOpenStatic, adLockReadOnly, adCmdTableDirect







____________________________
multi-tasking - the ability to hang more than one app. at the same time.

11-04-2006 at 03:11 PM
View Profile Send Email to User Show All Posts | Quote Reply
VbPanicStation
Level: Big Cheese

Registered: 13-12-2004
Posts: 21
icon Re: Connection String and SQL to retrieve records

Thanks VB Lord!
May I please ask another silly question? How can you test to check what data you have retrieved inthe sql statement? Can you use MsgBox strSQL or print to a window?

11-04-2006 at 08:38 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Connection String and SQL to retrieve records

Hi,
If you just want to make sure that you have got some data in the recordset before you try to do something with it then you can test to see where the cursor is sitting. If it is at both BOF and EOF then there are no records -

If Not (rst.BOF And rst.EOF) Then
'you have records
rst.MoveFirst
Do Until rst.EOF
..  do something
..
..
rst.MoveNext
Loop
End If


If you want to see exactly what is in the Recordset then you could read the data into a String Variable as you go through the Loop (strData = strData & " - " & rst("Fieldname") ) and then display it in a MessageBox ( MsgBox strData) or you could put a Debug.Print rst("Fieldname") command in the Loop and then check the immediate pane to see what you got.
Does that help?




____________________________
multi-tasking - the ability to hang more than one app. at the same time.

12-04-2006 at 08:30 AM
View Profile Send Email to User Show All Posts | Quote Reply
VbPanicStation
Level: Big Cheese

Registered: 13-12-2004
Posts: 21
icon Re: Connection String and SQL to retrieve records

Thank you so much Geoffs. You truely are a VB Lord!!

Code you gave me - works a treat - exactly what I wanted to do.

Thanks again

Vbpanic (now relaxed & chilled out!)

12-04-2006 at 02:57 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Connection String and SQL to retrieve records Solved Topic
Previous Topic (Please Help- Need simple VB code (similar to Macro))Next Topic (HELP on exporting data from excel to word) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder