473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Data mismatch message

39
I am having trouble getting my SQL string to operate correctly. I have modified an online application for my use but I think because the fields were all text. I continuously receive an Error 13: Type mismatch.

I have three list boxes, one to capture the ID number (number), the City (text) and Area (text).
There is a button that pulls all the responses in the following variables:

strID
strCity
strArea


The SQL string is the following:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT tbMaster_A.* FROM tbPrograms " & _
  2.          "WHERE tbMaster_A.[GrID] = " & strID & _
  3.          strCityCondition & "tbMaster_A.[City]" & strCity & _
  4.              strAreaCondition & "tbMaster_A.[Area]" & strArea & "';"
  5.  
  6.  
The following variables represent the users selection of AND or OR when creating the query. So if the user selects the AND option button for a query, then the following string variables becomes an AND (or OR).

strCityCondition
strAreaCondition


I thought by making the string equaling the variable would resolve it. When I removed the equals sign and replaced with the ampersand &, to concatenate the string.

There was no change.


How can I make the [GrID] string accept a numeric user entry?

Thanks.
Aug 18 '11 #1
10 2061
Rabbit
12,516 Expert Mod 8TB
If this is how you're building youre SQL string,
Expand|Select|Wrap|Line Numbers
  1. "SELECT tbMaster_A.* FROM tbPrograms " & _ 
  2. "WHERE tbMaster_A.[GrID] = " & strID & _ 
  3. strCityCondition & "tbMaster_A.[City]" & strCity & _ 
  4. strAreaCondition & "tbMaster_A.[Area]" & strArea & "';"
Assuming
Expand|Select|Wrap|Line Numbers
  1. strID = 1
  2. strCity = LA
  3. strCityCondition = Okay
  4. strArea = East
  5. strAreaCondition = Bad
Then here's what it would output
Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster_A.* FROM tbPrograms WHERE 
  2. tbMaster_A.[GrID] = 1OkaytbMaster_A.[City]LABadtbMaster_A.[Area]East';
The extra line in there was only for formatting purposes.
Aug 18 '11 #2
Qtip23
39
@Rabbit
Yep, I see where you are going. Actually, the and strAreaCondition and strCityCondition represents the selection from the option button.

These conditions represent a logical AND or OR.

So let's say we have the following string variable values,
Expand|Select|Wrap|Line Numbers
  1. strID = 2 
  2. strCity = LA 
  3. strCityCondition = AND 
  4. strArea = East
  5. strAreaCondition = AND
  6.  
The query should report all the County information who fit the strID equal to 2.

When i run the query in the Access database window, I have no problems.

I use the following:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster_A.*
  2. FROM tbPrograms
  3. WHERE (((tbMaster_A.[GrID]=2) AND ((tbMaster_A.[City]) Like '*') AND ((tbMaster_A.[Area]) Like '*'));
  4.  
So maybe it is something else, right?

Thanks,
Aug 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
No, the problem stays the same. Given that the condition variables are logical operators. What you get is
Expand|Select|Wrap|Line Numbers
  1. SELECT tbMaster_A.* FROM tbPrograms WHERE  
  2. tbMaster_A.[GrID] = 1ANDtbMaster_A.[City]LAANDtbMaster_A.[Area]East';
Aug 18 '11 #4
Qtip23
39
Ok, so I think I am getting closer to the answer.

I did forget to mention that this query is based on a one to many relationship.

So, the tbPrograms is on the one side and tbMaster_A is on the many side. They are related by the GrPrgID field which is the PK of tbPrograms and FK of the tbMaster_A table.

I went back to look at the query in Access, so now I have the following:

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [forms]![frmA]![lstGr] Long;
  2. SELECT tbMaster_A.*,tbPrograms.ID, tbPrograms.Region, tbPrograms.StateReg
  3. FROM tbPrograms INNER JOIN tbMaster_A ON tbPrograms.GrPrgID = tbMaster_A.GrPrgID
  4. WHERE (((tbMaster_A.GrPrgID)=[forms]![frmA]![lstGr])) AND ((tbMaster_A.Region) Like "*") AND ((tbMaster_A.StateReg) Like "*"));
  5.  
When I actually place values in the place ID, Region or State, then I receive a query output.

It's not looking good on the coding end. Can an inner join statement be placed in VBA code when building a SQL string?
Aug 18 '11 #5
Rabbit
12,516 Expert Mod 8TB
Yes, you can build any SQL string in VBA code.
Aug 18 '11 #6
Qtip23
39
So while I am not getting any syntax error messages on my SQL string after clicking on button.
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT tbMaster_A.* FROM tbPrograms " & _
  2.              "INNER JOIN tbMaster_A ON tbPrograms.GrPrgID=tbMaster_A.GrID " & _
  3.              "WHERE tbPrograms.[GrID] = " & strID & _
  4.              strCityCondition & "tbMaster_A.[City] " & strCity & _
  5.              strAreaCondition & "tbMaster_A.[Area] " & strArea & ";"
  6.  
However, I am getting a run-time error message 94: Invalid use of Null. Any ideas on why I am receiving this message, especially since I thougt I removed any text references in my SQL string?

Expand|Select|Wrap|Line Numbers
  1. ' Build criteria string for Program
  2.     For Each varItem In Me.lstGr.ItemsSelected
  3.         'strID = strID & ",'" & Me.lstGr.ItemData(varItem) & "'"
  4.         strID = Me.lstGr.ItemData(varItem)
  5.     Next varItem
  6.  
  7.     If Len(strID) = 0 Then
  8.         'strID = "Like '*'"
  9.         strID = Me.lstGr.ItemData(varItem)
  10. '    Else
  11. '        strID = Right(strID, Len(strID))
  12. '        strID = "IN(" & strID & ")"
  13.     End If
  14. ' Build criteria string for City
  15.     For Each varItem In Me.lstCity.ItemsSelected
  16.         strCity = strCity & ",'" & Me.lstCity.ItemData(varItem) & "'"
  17.     Next varItem
  18.     If Len(strCity) = 0 Then
  19.         strCity = "Like '*'"
  20.     Else
  21.         strCity = Right(strCity, Len(strCity) - 1)
  22.         strCity = "IN(" & strCity & ")"
  23.     End If
  24. ' Build criteria string for Area
  25.     For Each varItem In Me.lstArea.ItemsSelected
  26.         strArea = strArea & ",'" & Me.lstArea.ItemData(varItem) & "'"
  27.     Next varItem
  28.     If Len(strArea) = 0 Then
  29.         strArea = "Like '*'"
  30.     Else
  31.         strArea = Right(strArea, Len(strArea) - 1)
  32.         strArea = "IN(" & strArea & ")"
  33.     End If
  34. ' Get City condition
  35.     If Me.optAndRegion.Value = True Then
  36.         strCityCondition = " AND "
  37.     Else
  38.         strCityCondition = " OR "
  39.     End If
  40. ' Get Area condition
  41.     If Me.optAndState.Value = True Then
  42.         strAreaCondition = " AND "
  43.     Else
  44.         strAreaCondition = " OR "
  45.     End If
  46.  
Thanks,
Aug 18 '11 #7
Rabbit
12,516 Expert Mod 8TB
You should print out the whole sql to debug and then see if that runs in a regular query. That will tell us whether the issue lies with the query string or with the VBA code.
Aug 18 '11 #8
NeoPa
32,556 Expert Mod 16PB
Lots can be done with a SQL string when working in VBA. Have a quick look at How to Debug SQL String and think about comparing the SQL in your QueryDef with the SQL in the string as produced by your code.

Are they similar?
Where do they differ?
Aug 18 '11 #9
Qtip23
39
@Rabbit @NeoPa
I found the link discussing the testing/debugging items to be a wealth of information, especially the use of the debug.print throughout my code.

I have been using it to see the values of my variables and form control selections. What I have gathered based on my complex queries is that I need to rethink my table relationships. So for now, I am going back to the drawing board (for a minute).

Thanks
Aug 19 '11 #10
NeoPa
32,556 Expert Mod 16PB
Sounds like a very intelligent and sensible reaction to me. I'm glad you found the link helpful :-)
Aug 19 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Karl Irvin | last post by:
I copied the following code from the MS website. It compiles OK but gives a type mismatch message on the line that says: Set Flds = iConf.Fields What needs to be changed? Also can I use this to...
1
by: Mo | last post by:
The following select statement is givign me a 'data type mismatch' message and I don't really know why. strSQL = "SELECT CN" strSQL = strSQL & " FROM tblDeaths" strSQL = strSQL & " WHERE CN = ...
3
by: Jake | last post by:
I am currently trying to create my own Point Of Sale software for my retail store. I wrote the program with the UPC field as Long integer. When I started to add the products by UPC code, I got a...
5
by: kjworm | last post by:
Hello Everyone, I have been fighting with a type mismatch error for many hours today and I can't seem to find what the problem is. Hopefully it is more than a missing apostrophe! I have isolated...
1
by: Maika | last post by:
lstSearchDoctor.ItemData(lstSearchDoctor.NewIndex) = mrsDoctorList!DoctorCode Where DoctorCode as String lstSearchPatient.ItemData(lstSearchPatient.NewIndex) = mrsPatientList!PatientNum ...
2
by: irkahs | last post by:
Dear friends, I am connecting an ASP page to an Access table. The table in Access has a field called orderNo which is of type "Number". Now...what I want to do is design an SQL...
1
by: =?Utf-8?B?S3VtYXIuQS5QLlA=?= | last post by:
I am having a problem in reading the values from the excel sheet in ASP .Net. I am using the following connection string to read values from rows from an excel sheet into a dataset. One of the...
2
by: kwokv616 | last post by:
=( what is wrong with this... when i run the query it says "data type mismatch in criteria expression"... Field aprid: Sum((++)*1*IIf((Not (=) And Not (=("TOPUP" Or "FSB" Or "FSSB" Or "MKB")))...
5
by: trey3143 | last post by:
Have this code to check for duplicates and if I change my BoxID to a text and run it as a string it works, but I need it to be a Long Integer. The data mismatch I am getting is underlined there are...
10
by: rouny4u | last post by:
Hello, I started working on VBA,access from yesterday, and here I am with my first problem :). Please help. I get the above mentioned problem when my UDF is returning an integer.(Following is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.