|
-
Nov 3rd, 2011, 01:49 PM
#1
Thread Starter
Lively Member
[RESOLVED] Write Query Loop on Excel worksheet in VBA module
Is it possible to set up and write SQL query on a worksheet? loop thru the record set and insert data onto another worksheet in the same file. If so, please show me a sample snippet on how to set it up in the VBA modules.
-
Nov 5th, 2011, 07:15 AM
#2
Re: Write Query Loop on Excel worksheet in VBA module
http://support.microsoft.com/kb/257819
check out this article from microsoft
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 8th, 2011, 12:33 PM
#3
Thread Starter
Lively Member
Re: Write Query Loop on Excel worksheet in VBA module
I am getting an error when it gets to the oCm.Execute line for some reason. Can you please look at my code and see if I am missing something. I am using ADO 2.8 library refrences in the excel VBA module.
vb Code:
Dim Cn As ADODB.Connection '* Connection String
Dim oCm As ADODB.Command '* Command Object
Dim iRecAffected As Integer
' On Error GoTo ADO_ERROR
Set Cn = New ADODB.Connection
DBPath = "S:\Common\DialerExtracts\KAUFFMAN\dialer_result_last6attempts.mdb"
Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & DBPath
Cn.ConnectionTimeout = 40
Cn.Open
Set oCm = New ADODB.Command
oCm.ActiveConnection = Cn
SQL$ = "INSERT INTO tbl_results (record_num) VALUES (" & record_num & ")"
oCm.CommandText = SQL$
oCm.Execute
-
Nov 8th, 2011, 12:54 PM
#4
Thread Starter
Lively Member
Re: Write Query Loop on Excel worksheet in VBA module
I figured out what was wrong. I was getting a primary key error, once I added in the error checking. Marked as resolved.
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
|