|
-
Feb 1st, 2006, 03:05 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] VBA and Executing SQL Statements
Quick question... what do you need for connection info to execute an sql statement from within Access VBA? Can I do it just like using ADO in VB?
-
Feb 1st, 2006, 03:08 PM
#2
Re: VBA and Executing SQL Statements
Yes, you can use ADO in Access VBA, pretty much exactly as in VB.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 1st, 2006, 03:09 PM
#3
Thread Starter
Frenzied Member
Re: VBA and Executing SQL Statements
what would I use for the connection? Is there a variable to automatically connect to the current DB?
-
Feb 1st, 2006, 03:19 PM
#4
Re: VBA and Executing SQL Statements
First off. you will need to add a reference to the Microsoft ActiveX Data Objects library.
To create recordsets from the current DB you would use the following syntax
VB Code:
Dim rsMyRecordSet As ADODB.Recordset
Dim sSQL As String
Set rsMyRecordSet = New ADODB.Recordset
sSQL = "SELECT field FROM table....etc"
rsMyRecordSet.Open sSQL, CurrentProject.Connection
the "CurrentProject.Connection" in the above connects to the db in which your code resides.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 1st, 2006, 03:20 PM
#5
Re: VBA and Executing SQL Statements
You can use this to execute a sql string.
VB Code:
Public Function ExecuteMe(ByVal sSQL As String)
Application.CurrentDb.Connection.Execute sSQL
End Function
With a few changes you can make it a Command Object too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 1st, 2006, 03:25 PM
#6
Re: VBA and Executing SQL Statements
And this one to open a recordset
VB Code:
Public Function OpenMe(ByVal sSQL As String) As Recordset
Dim rs As Recordset
Set rs = Application.CurrentDb.OpenRecordset(sSQL, dbOpenDynamic, , dbOptimistic)
OpenMe = rs
End Function
Sorry for the delay, had a phone call.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 1st, 2006, 03:34 PM
#7
Thread Starter
Frenzied Member
Re: VBA and Executing SQL Statements
Ok I have tried all 3 of you guys' suggestions. However I got errors on all of them. The last one I tried was
VB Code:
Dim rs As Recordset
Set rs = Application.CurrentDb.OpenRecordset(sSQL, dbOpenDynamic, , dbOptimistic)
OpenMe = rs
I would like to open it as a recordset but I got an error on the Set statement:
Invalid Arguement
-
Feb 1st, 2006, 03:36 PM
#8
Re: VBA and Executing SQL Statements
1/ Have you added a reference to the Microsoft ActiveX Data Objects library?
2/ Change
Dim rs as Recordset
to
Dim rs as ADODB.Recordset
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 1st, 2006, 03:39 PM
#9
Thread Starter
Frenzied Member
Re: VBA and Executing SQL Statements
Yes I have referenced Microsoft ActiveX Data Objects 2.1 Library and this is the exact code I am using:
Dim rs As ADODB.Recordset
Dim stSQL As String
stSQL = "SELECT * FROM PIU_Percentages"
Set rs = Application.CurrentDb.OpenRecordset(stSQL, dbOpenDynamic, , dbOptimistic)
OpenMe = rs
-
Feb 1st, 2006, 03:47 PM
#10
Thread Starter
Frenzied Member
Re: VBA and Executing SQL Statements
Got this to work:
Dim rs As ADODB.Recordset
Dim stSQL As String
stSQL = "SELECT * FROM PIU_Percentages"
Set rs = New ADODB.Recordset
rs.Open stSQL, CurrentProject.Connection
Dunno why it didn't work the first time I tried it but thanks for your help guys!
-
Feb 1st, 2006, 04:01 PM
#11
Re: [RESOLVED] VBA and Executing SQL Statements
Try the following...
VB Code:
Sub test()
Dim rs As ADODB.Recordset
Dim sSQL As String
Set rs = New ADODB.Recordset
sSQL = "SELECT * FROM PIU_Percentages"""
rs.Open Source:=sSQL, ActiveConnection:=CurrentProject.Connection, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic
rs.MoveFirst
MsgBox rs.Fields(0).Value
rs.Close
Set rs = Nothing
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|