Results 1 to 5 of 5

Thread: Assigning value from SQL statement to a variable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    Assigning value from SQL statement to a variable

    Can anyone help me assign the value of an SQL statement to a variable...

    Here is what I have so far:


    Code:
    Dim strID As Recordset    
    Set strID = New Recordset
    
    strID.Source = "SELECT tblLIST_CS_EMP.ID " & _
                            "FROM tblLIST_CS_EMP WHERE tblLIST_CS_EMP.Full_Name = " & Me.cboName & ""
        
    MsgBox strID.Source
    The current output is the whole SQL statement with an equals sign and the coreect ID.

    So, I am stuck on how to pull just the ID out of the recordset.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Actually this should be a question for the Databases forum,
    but if I understand your question correctly you want to retrieve
    an id field based upon an entered id value in the combo box?

    VB Code:
    1. Dim strID As Recordset    
    2. Set strID = New Recordset
    3.  
    4. strID.Source = "SELECT tblLIST_CS_EMP.ID " & _
    5.                "FROM tblLIST_CS_EMP WHERE tblLIST_CS_EMP.ID = " & Me.cboID.Text
    6.    
    7. MsgBox strID.Source
    Assuming that ID field is a numeric and this is for Access or SQL.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    Sorry...

    ...didn't notice a database section.

    Actually, I want to retrieve and ID field based upon an entered Full_Name in a combo box.

    Basically I have 2 combo boxes, one cboName, and one cboID.

    The user can use either one to select the person they are looking for.

    However, when the full name is chosen, I want the cboID to show the appropriate ID, and vice versa.

    I tried your code, with the adjustment:
    Code:
        Dim strID As Recordset
        Set strID = New Recordset
        
        strID.Source = "SELECT tblLIST_CS_EMP.L_ID " & _
                       "FROM tblLIST_CS_EMP WHERE tblLIST_CS_EMP.Full_Name = " & Me.cboName.Text
    And the msgbox is just displaying the SQL code with value that is in the cboName.Text.

    I am using an Access database and putting this code within a form in that access database,

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Use the fields collection of the recordset. Modify your sql statement
    for which cbo the user is using. Then populate the other cbo like
    this...

    VB Code:
    1. Option Compare Database
    2.  
    3. Private Sub cboName_Click()
    4.  
    5.     Dim strID As Recordset
    6.    
    7.     Set strID = New Recordset
    8.    
    9.     strID.Source = "SELECT tblLIST_CS_EMP.L_ID " & _
    10.     "FROM tblLIST_CS_EMP WHERE tblLIST_CS_EMP.Full_Name = " & Me.cboName.Text
    11.     strID.OpenRecordset dbOpenDynamic, dbReadOnly
    12.    
    13.     Me.cboL_ID.AddItem strID.Fields("L_ID").Value
    14.    
    15. End Sub
    16.  
    17. Private Sub cboID_Click()
    18.  
    19.     Dim strID As Recordset
    20.    
    21.     Set strID = New Recordset
    22.    
    23.     strID.Source = "SELECT tblLIST_CS_EMP.Full_Name " & _
    24.     "FROM tblLIST_CS_EMP WHERE tblLIST_CS_EMP.L_ID = " & Me.cboID.Text
    25.     strID.OpenRecordset dbOpenDynamic, dbReadOnly
    26.    
    27.     Me.cboName.AddItem strID.Fields("Full_Name").Value
    28.    
    29. End Sub
    You will need to add error trapping and a little more logic to
    complete your procedures.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    406

    hmmm....

    ...Getting this error:

    "Method or datamember not found" on this code:
    .OpenRecordset
    on this line:
    strID.OpenRecordset dbOpenDynamic, dbReadOnly

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