Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
CLamar
 
Posts: n/a
Default How to pass a variable into an SQL statement

I am trying to pass a variable into an SQL statement as part of the criteria.
Here is what my code looks like: The ** indicate where the SQL statement is.
Selec is the the variable i am trying to pass instead of hardcoding it.

Public Sub QryBatch_Click()
Dim Selec As Variant
Dim Cnct As String, Src As String
Dim Connection2 As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer
Dim Row As Integer
Dim w As Integer

Sheets("Sheet3").Cells.Clear

'Assigns the selected value inside the List Box to a variable
For w = 0 To Sheets("Sheet1").LstBatchnum.ListCount - 1
If Sheets("Sheet1").LstBatchnum.Selected(w) Then _
Selec = Selec & Sheets("Sheet1").LstBatchnum.List(k) & vbCrLf
Next w

'Delete Previous Contents in the Tire number ListBox
For i = 1 To Worksheets("Sheet1").LstTirenum.ListCount
Sheets("Sheet1").LstTirenum.RemoveItem (0)
Next i

Set Connection2 = New ADODB.Connection
Cnct = "Driver={Microsoft Access Driver (*.mdb)};Dbq=O:\SITERW\Carl
Lamar\Access\databases\ely.mdb;Uid=Admin;Pwd=;"
Connection2.Open ConnectionString:=Cnct

Set Recordset = New ADODB.Recordset
With Recordset
**Src = "DEFINE Selec SELECT * FROM tblResults WHERE Batch =
'&Selec'"
.Open Source:=Src, ActiveConnection:=Connection2

'For Col = 0 To Recordset.Fields.Count - 1
' Sheets("Sheets3").Range("A1").Offset(0, Col).Value =
Recordset.Fields(Col).Name
'Next

Sheets("Sheet3").Range("A1").Offset(1, 0).CopyFromRecordset
Recordset

'Copies the data inside the recordset into a List Box
For Row = 2 To Recordset.Properties.Count - 1
Sheets("Sheet1").LstTirenum.AddItem
Sheets("Sheet3").Cells(Row, 4)
Next Row

End With
Set Recordset = Nothing
Connection2.Close
Set Connection2 = Nothing


End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aling multiple sets of data by header column MarkusO Excel Discussion (Misc queries) 2 April 12th 06 07:29 PM
SET statement tutorial Daminc Excel Discussion (Misc queries) 13 January 17th 06 04:47 PM
Do I need a sumif or sum of a vlookup formula? PeterB Excel Worksheet Functions 0 June 1st 05 12:23 PM
How to (re)set a range.value to pass -0- to a "double" variable Dennis Excel Discussion (Misc queries) 2 April 15th 05 11:13 AM
Add 2nd variable to if statement - ifAnd? Todd F. Excel Worksheet Functions 4 December 18th 04 07:49 PM


All times are GMT +1. The time now is 03:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"