Solved

SQL between query and MS Access Help in VB

Posted on 2006-11-24
11
1,153 Views
Last Modified: 2013-12-25
Hey there Experts

I have a SQL query that fills a record set and then displays the result in a text box but the query is not working the syntax I think is right but the record set does not get filled.

Here is the code

Dim sqlstring As String
Dim myRS As New ADODB.Recordset

Set myConnection = New ADODB.Connection
myConnection.Open "PROVIDER=Microsoft.jet.OLEDB.4.0;data source=" & App.Path & "\Database\Database.mdb" & ";"

myRS.CursorLocation = adUseClient
sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE TREAT.DATE BETWEEN " & CDate(CustomStartDate) & " AND " & CDate(CustomEndDate) & ""
Set myRS = myConnection.Execute(sqlstring)

       
        Do Until myRS.EOF
        If IsNull(myRS!Street2) = True Then
        txtResults.SelText = myRS!CFIRST + " " + myRS!CSURNAME + " " + myRS!Street + vbCrLf
        Else
        txtResults.SelText = myRS!CFIRST + " " + myRS!CSURNAME + " " + myRS!Street + " " + myRS!Street2 + vbCrLf
        End If
        myRS.MoveNext
        Loop
       
 myRS.Close

Thanks

Nothern_Lights
0
Comment
Question by:Nothern_Lights
11 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 18009262
try using hashes

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE TREAT.DATE BETWEEN #" & CDate(CustomStartDate) & "# AND #" & CDate(CustomEndDate) & "#"

other way is to use format

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE Format(TREAT.DATE,'YYYYMMDD') BETWEEN " & Format(CDate(CustomStartDate),"YYYYMMDD") & " AND " & Format(CDate(CustomEndDate),"YYYYMMDD")

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE Format(TREAT.DATE,'YYYYMMDD') >= " & Format(CDate(CustomStartDate),"YYYYMMDD") & " AND Format(TREAT.DATE,'YYYYMMDD')  <= " & Format(CDate(CustomEndDate),"YYYYMMDD")
0
 

Author Comment

by:Nothern_Lights
ID: 18009353
hey rockiroads

The code you gave me is just displaying the whole client table multiple times.

What I need the SQL query to do is to take the customstartdate and the customenddate and search the client and treat table and findout what clients have visited the shop within the dates specified

Nothern_Lights
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 18009362
ok, what links the client and the treat table?
u need to perform the join

SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2
FROM CLIENT, TREAT
WHERE CLIENT.somefeld = TREAT.somefield


then u do the filter on the TREAT date
use the filter that worked for u, but dont start with WHERE, put a AND as we already have a WHERE

0
 

Author Comment

by:Nothern_Lights
ID: 18009367
So would the query be

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2,MAX(TREAT.DATE) FROM CLIENT, TREAT WHERE CLIENT.CCODE = TREAT.CCODE AND TREAT.DATE BETWEEN #" & CDate(CustomStartDate) & "# AND #" & CDate(CustomEndDate) & GROUP BY CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 "

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 18009403
Yes, u need to join the two fields and use the appropriate filter on the dates that worked for you

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Nothern_Lights
ID: 18009454
Ok I have made my query but it does not fill the recordset

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE CLIENT.CCODE = TREAT.CCODE AND TREAT.DATE >= " & CDate(CustomStartDate) & " AND TREAT.DATE <= " & CDate(CustomEndDate) & " GROUP BY CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2"

and i have tried

sqlstring = "SELECT CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2 FROM CLIENT, TREAT WHERE CLIENT.CCODE = TREAT.CCODE AND TREAT.DATE >= #" & CDate(CustomStartDate) & "# AND TREAT.DATE <= #" & CDate(CustomEndDate) & "# GROUP BY CLIENT.CSURNAME,CLIENT.CFIRST,CLIENT.STREET,CLIENT.STREET2"

HELP btw i have never used SQL b4

Nothern_Lights
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 18010509
I should really concentrate more!
I didnt notice how u opened the recordset, yours is incorrect, .Execute is used for action queries like insert/delete
for select's u need to use Open
eg

    Dim sqlstring As String
    Dim myConnection As ADODB.Connection
    Dim myRS As New ADODB.Recordset
   
    Set myConnection = New ADODB.Connection
    myConnection.Open "PROVIDER=Microsoft.jet.OLEDB.4.0;data source=" & app.Path & "\Database\Database.mdb" & ";"
   
    sqlstring = "YOUR FAVOURED SQL STRING HERE"

'SETUP RECORDSET    
    myRS.CursorType = adOpenKeyset
    myRS.LockType = adLockPessimistic
    myRS.CursorLocation = adUseClient
    myRS.Open sqlstring
   
'WHY ARE U USING selText FOR
    Do Until myRS.EOF
        If IsNull(myRS!Street2) = True Then
            txtResults.SelText = myRS!CFIRST + " " + myRS!CSURNAME + " " + myRS!Street + vbCrLf
        Else
            txtResults.SelText = myRS!CFIRST + " " + myRS!CSURNAME + " " + myRS!Street + " " + myRS!Street2 + vbCrLf
        End If
        myRS.MoveNext
    Loop
   
    myRS.Close
    Set myRS = Nothing
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 18025553
Rockiroads's tutorial is clearly and easy to understand.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18034604
>>.Execute is used for action queries like insert/delete for select's u need to use Open<<
You may want to double check this.  There is nothing wrong with using Execute to return a recordset.  In fact, providing a firehose (forward-only, read-only) cursor can be used it will give you the best performance.
0
 

Author Comment

by:Nothern_Lights
ID: 18037847
Hey there rockiroads

I put in the code you posted and added my sqlstring i got an error at myRS.Open sqlstring saying

Run-time error `3709`:

"The connection cannot be used to perform this operation. It is wither closed or invalid in this context"
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 18085450
Sorry mate, I stopped using EE for a short while then had a massive catch up.

Regarding this error, I forgot to add the connection object to when u open the recordset


it should be

    myRS.Open sqlstring, myConnection



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now