Results 1 to 6 of 6
  1. #1
    JFo is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    393

    Simple SQL statement in VB

    Just trying to add the selection from a combo box into the dialog prompt/ error catcher message, however need help with my SQL command

    Dim x As Integer
    Dim NewData As String
    Dim strSQL As String

    NewData = Me.cmbEquipType.Value()
    strSQL = "SELECT tblEquipmentType([EquipTypeName]) FROM tblEquipmentType" & _
    " WHERE [tblEquipmentType].EquipTypeID = newData ;"


    x = MsgBox("Do you want to add a" & Chr(34) & strSQL & _
    Chr(34) & "EquipType to this inspection?", 4, "Exit?")

    As you can see at the moment it actually prints to screen the SQL statement

    I can get it to print to screen the combo value with

    x = MsgBox("Do you want to add a" & Chr(34) & newData & _
    Chr(34) & "EquipType to this inspection?", 4, "Exit?")

    however because the bound column is the ID which I need for other things I simply need an SQL statement in vb to get the Name

    I am fairly good with SQL in stand alone sense however I struggle using SQL statements in VB

    Thanks

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    For debugging, I'd recommend you use a number of Debug.Print variable_name in your code. This will provide info on the immediate window and allow you to visually check the contents of various variables before actually executing some sql.

    eg Debug.print strSQL

    I haven't tested this but my view is that

    strSQL = "SELECT tblEquipmentType.EquipTypeName FROM tblEquipmentType" & _
    " WHERE [tblEquipmentType].EquipTypeID = newData ;"
    should be

    strSQL = "SELECT tblEquipmentType([EquipTypeName]) FROM tblEquipmentType" & _
    " WHERE [tblEquipmentType].EquipTypeID = " & newData & ";"

    This would evaluate the value of newData and substitute it into the SQL string.

    A Debug.print strsql would tell you for sure.
    Last edited by orange; 10-04-2011 at 08:12 PM.

  3. #3
    JFo is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    393
    Thanks for your feed-back, not entirely sure how to use Debug.Print strSQL I put this in my code a number of times however it does nothing when I run normally or when I compile database.

    I already know that newData is getting the correct EquipTypeID from the combo box I just want an SQL statement that converts the EquipTypeID to the EquipTypeName however I don't know how to do this in VB

    This is correct SQL for my statement

    SELECT tblEquipmentType.EquipTypeName FROM tblEquipmentType WHERE [tblEquipmentType].EquipTypeID =Forms!frmEquipment.EquipTypeID;

    Also is there any application that can convert SQL to VB(Using SQL)? Or anywhere resources that could be recommended

    Thanks

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Presuming the other field is in the combo row source, simply:

    MsgBox "Do you really want to add a " & Me.cmbEquipType.Column(1)

    or whatever the appropriate column number is.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    JFo is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    393
    excellent that's it... no need to use sql an SQL statement if you extract the value like that, thanks


  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    No problem; simpler is usually better.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  2. Replies: 1
    Last Post: 07-30-2011, 07:58 AM
  3. Replies: 0
    Last Post: 10-21-2010, 08:24 AM
  4. is this a valid SQL statement ? (simple)
    By markjkubicki in forum Queries
    Replies: 1
    Last Post: 07-22-2010, 06:49 PM
  5. Replies: 2
    Last Post: 08-26-2009, 10:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums