Results 1 to 6 of 6

Thread: data types

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    115

    data types

    hey all hope u can help me wif this prob

    ok i got a variable

    Code:
    Dim intID As Integer
    then i put a value in it

    Code:
    intID = lvwContacts.SelectedItem.Text
    then i open a database lookin for that value

    Code:
    RS.Open "Select * from tblPcontacts where id='" & intID & "'", CN, adOpenStatic, adLockOptimistic
    NOW

    the problem i am having is:

    "Data type mismatch in criteria expression"

    now the data type in the database is "auto number"

    i have tried doing this

    Code:
    intID = CInt(lvwContacts.SelectedItem.Text)
    but i stil get the same error

    can some1 plz help

    oh also if i do

    Code:
    RS.Open "Select * from tblPcontacts where id=1", CN, adOpenStatic, adLockOptimistic
    put a number instead of variable it works

    plz help

    tnx

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Just get rid of the single quotes in your query string.

    Code:
    RS.Open "Select * from tblPcontacts where id = " & intID, CN, adOpenStatic
    Also ideally you should declare the intID variable as String, because you are storing a Text property to it.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Junior Member
    Join Date
    Oct 2001
    Posts
    24
    try this....

    [CODE]
    Dim strID as string
    strID = lvwContacts.SelectedItem.Text
    RS.Open "Select * from tblPcontacts where id= '" & intID & "' ORDER BY id", CN, adOpenStatic, adLockOptimistic

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by boylesw
    try this....

    [CODE]
    Dim strID as string
    strID = lvwContacts.SelectedItem.Text
    RS.Open "Select * from tblPcontacts where id= '" & intID & "' ORDER BY id", CN, adOpenStatic, adLockOptimistic
    That will fail, because Autonumber fields are of Long Integer data type, at least in Access. When you include the single quotes in your query around the ID variable's value, the query will interpret it as a string value, because in SQL a string value is delimited in single quotes. Since the Autonumber really is a long integer, using a string value for comparison will produce the Data Type Mismatch error.

    Also you are using strID in the declaration, but intID in the query

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    115
    ty all for yor help

    removing the single quotes fixed it

    tnx

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    It should be:
    VB Code:
    1. Dim lngID   As Long
    2.  
    3.    lngID = CLng(lvwContacts.Selectedtem.Text)
    If ID is an int in the database, then in fact it's a LONG in VB, not an interger.
    Just because you store the varible in a text field does not mean you define it as strID As String, very bad, since it is actually a long...

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width