Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strSQL with multiple criteria problem

Posted on 2011-09-14
12
Medium Priority
?
852 Views
Last Modified: 2012-05-12
I have a string with multiple and cannot get it to work.  It will work with any two of the criteria but not the third.  I have tried so many different ways.  What am I missing?

Dim sLoadLoc as String
Dim sLoadNum as Variant
Dim sLoadLine as Variant

    sLoadNum = Forms![frm_bol_hed]![txt_loadNum]
    sLoadLoc = Forms![frm_bol_hed]![txt_loc]
    sLineNum = Me.LD_DET_LINE

            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = '" & sLoadLoc & "' And [LD_DET_LINE] =" & sLineNum And [LD_DET_NUM] = " & sLoadNum"
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
0
Comment
Question by:pskeens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 75
ID: 36540461

Try this:



            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = " & Chr(34) & sLoadLoc &  Chr(34) & " And [LD_DET_LINE] =" & Chr(34) & sLineNum & Chr(34) & " And [LD_DET_NUM] = " & Chr(34) & sLoadNum & Chr(34)
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

0
 
LVL 75
ID: 36540466
btw ... Chr(34) is a Double Quote

0
 
LVL 2

Author Comment

by:pskeens
ID: 36540503
Negative

"Datatype mismatch in expression"

Same error I was getting before.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:pskeens
ID: 36540508
strLoadNUm and strLineNum are numeric types.  These should not be in double quotes, correct?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36540510
You should add the line "Option Explicit" to the top of any code module, right under Option Compare Database.  Doing so forces you to explicitly declare any variables used.

What data type is sLineNum?  numeric or text?  You don't have a Dim statement for it.

When writing SQL statements, you need to delimit text fields with quotes, dates with # marks, and numeric fields do not get delimited.

mx's code above assumes that all fields are text.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 36540523
"strLoadNUm and strLineNum are numeric types.  "
Then it would help if the naming convention was not implying String ... So ...


            strSQL = "select * from TBL_LOAD_DET where [LD_DET_LOC] = " & Chr(34) & sLoadLoc &  Chr(34) & " And [LD_DET_LINE] =" &  sLineNum  & " And [LD_DET_NUM] = " & sLoadNum  
        Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
0
 
LVL 75
ID: 36540536
In fact, you have

Dim sLoadLoc as String
Dim sLoadNum as Variant
Dim sLoadLine as Variant

Then


    sLoadNum = Forms![frm_bol_hed]![txt_loadNum]
    sLoadLoc = Forms![frm_bol_hed]![txt_loc]
    sLineNum = Me.LD_DET_LINE

So ... because of the 's' prefix ... seems to imply String.

mx
0
 
LVL 2

Author Closing Comment

by:pskeens
ID: 36540538
Winner winner, chicken dinner!  You the man!  I see what I missed now.  Stupid ampersands!!!  haha.

Thanks
0
 
LVL 75
ID: 36540551
Can we make that fish, as I am a Fishetarian.

And how about we work on those naming conventions :-)

mx
0
 
LVL 2

Author Comment

by:pskeens
ID: 36540787
naming conventions?  Which ones?
0
 
LVL 75
ID: 36540809
The ones that have a prefix of 's' (which would imply String) that are actually numeric ...

mx
0
 
LVL 2

Author Comment

by:pskeens
ID: 36540878
Oh yeah, that may be a good thing.  I will change after this application is done. lol.  I don't want to go back and change all the code in this thing.  Maybe after its complete then I will go back and clean up.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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