Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2008, 12:46 PM
Registered User
 
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing date variables into a sql statement

Hi all,

I'm having trouble with passing two variables into a SQL Statement. Using the following code i get an invalid column error. If I hardcode the dates, the query works fine. Any help would be greatly appreciated. Thanks.

'Set up connection to Oracle

Private Sub CommandButton1_Click()
Dim Conn As String
    Dim drv As String
    uid = "myUserName"
    pwd = "myPassword"
    dBase = "myDatabase"
    Set db = New ADODB.Connection

                Conn = "UID= " & uid & ";PWD=" & pwd & ";DRIVER={Microsoft ODBC For Oracle};" _
                & "SERVER=" & dBase & ";"

        'Screen.MousePointer = vbHourglass
        'Connect With ORACLE
        With db
            .ConnectionString = Conn
            .CursorLocation = adUseClient
            .Open
        End With
QueryDate
End Sub


'Perform the query and write the results into an Excel worksheet
Sub QueryDate()
Dim StartDate As String
Dim EndDate As String

StartDate = DTPicker1.Value
EndDate = DTPicker2.Value
StartQuery = Format(StartDate, "dd-" & "MMMM-" & "yyyy")
EndQuery = Format(EndDate, "dd-" & "MMMM-" & "yyyy")
StartQuery = UCase(StartQuery)
EndQuery = UCase(EndQuery)
BillingPeriod.Hide
MsgBox StartQuery


Dim i As Integer
sSql = "select unique wa_number, title from hours_cost_view where billing_date > StartQuery and billing_date < EndQuery"


'I have also tried this approach
'sSql = "select unique wa_number, title from hours_cost_view where billing_date > " & StartQuery & "and billing_date <" & EndQuery &""
Set rs = Nothing
rs.Open sSql, db, adOpenDynamic, adLockOptimistic


If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
i = 1
Do Until rs.EOF

Range("A2").CopyFromRecordset rs

Loop
rs.Close
End If
End Sub
 
Old May 1st, 2008, 01:05 PM
Registered User
 
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by hood8jmark
 Hi all,

I'm having trouble with passing two variables into a SQL Statement. Using the following code i get an invalid column error. If I hardcode the dates, the query works fine. Any help would be greatly appreciated. Thanks.

'Set up connection to Oracle

Private Sub CommandButton1_Click()
Dim Conn As String
    Dim drv As String
    uid = "myUserName"
    pwd = "myPassword"
    dBase = "myDatabase"
    Set db = New ADODB.Connection

                Conn = "UID= " & uid & ";PWD=" & pwd & ";DRIVER={Microsoft ODBC For Oracle};" _
                & "SERVER=" & dBase & ";"

        'Screen.MousePointer = vbHourglass
        'Connect With ORACLE
        With db
            .ConnectionString = Conn
            .CursorLocation = adUseClient
            .Open
        End With
QueryDate
End Sub


'Perform the query and write the results into an Excel worksheet
Sub QueryDate()
Dim StartDate As String
Dim EndDate As String

StartDate = DTPicker1.Value
EndDate = DTPicker2.Value
StartQuery = Format(StartDate, "dd-" & "MMMM-" & "yyyy")
EndQuery = Format(EndDate, "dd-" & "MMMM-" & "yyyy")
StartQuery = UCase(StartQuery)
EndQuery = UCase(EndQuery)
BillingPeriod.Hide
MsgBox StartQuery


Dim i As Integer
sSql = "select unique wa_number, title from hours_cost_view where billing_date > StartQuery and billing_date < EndQuery"


'I have also tried this approach
'sSql = "select unique wa_number, title from hours_cost_view where billing_date > " & StartQuery & "and billing_date <" & EndQuery &""
Set rs = Nothing
rs.Open sSql, db, adOpenDynamic, adLockOptimistic


If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
i = 1
Do Until rs.EOF

Range("A2").CopyFromRecordset rs

Loop
rs.Close
End If
End Sub
I was able to figure out where I erred and thought I'd post it back for other users. I simply needed to quote the single quotes around the date variables. See below:

Sub QueryDate()
Dim StartDate As String
Dim EndDate As String
Dim StartQuery As String
Dim EndQuery As String
StartDate = DTPicker1.Value
EndDate = DTPicker2.Value
StartQuery = Format(StartDate, "dd-" & "MMMM-" & "yyyy")
EndQuery = Format(EndDate, "dd-" & "MMMM-" & "yyyy")
StartQuery = UCase(StartQuery)
EndQuery = UCase(EndQuery)
BillingPeriod.Hide


Dim i As Integer
sSql = "select unique wa_number, title from hours_cost_view where billing_date >" & "'" & StartQuery & "' " & "and billing_date < " & "'" & EndQuery & "'" & ""
'sSql = "Select * from unique_inprogress_view where not exists(select null from unique_routed_wam_view where unique_inprogress_view.wa_number = unique_routed_wam_view.WA_NUMBER)"
Set rs = Nothing
rs.Open sSql, db, adOpenDynamic, adLockOptimistic


If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
i = 1
Do Until rs.EOF

Range("A2").CopyFromRecordset rs

Loop
rs.Close
End If
End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Variables silve1999 Pro Java 1 June 15th, 2006 12:54 PM
SQL Server XML - Passing Variables dkb XML 0 April 4th, 2005 03:43 AM
passing date time values to sql ashokparchuri ADO.NET 4 March 15th, 2005 01:47 AM
Passing a Global Variable Into a SQL Statement lwendt33 SQL Server DTS 1 February 2nd, 2005 05:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.