Results 1 to 8 of 8
  1. #1
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34

    Text Box Expression Builder with Queries

    I know how to do this in VB.NET but VBA is killing me!!!

    I have a form with a combo box. I would like to create an "on change" event where it updates the text boxes on that same form depending on what's been selected in the combo box.


    I don't know how to change the combo box's selected index so it defaults to the first item so there's no blanks/errors in my form.




    So when the combobox contents is changed, I'd like a routine that updates all the text boxes kind of like this:

    sub update()
    TextBox = "SELECT * FROM Table_Name WHERE ID =" & Combobox.text
    end sub


    Thanks in advance for your help!

  2. #2
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    Code:
    Option Compare Database
    Private Sub cbx_UserID_BeforeUpdate(Cancel As Integer)
    Call updateTextFields
    End Sub
    Private Sub updateTextFields()
    Dim DescrSQL As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    cbx_UserID.SetFocus
    DescrSQL = "SELECT Surveys.[Duty_Pos] FROM Surveys WHERE Surveys.[User_ID]='" & cbx_UserID.Text & "';"
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset(DescrSQL)
     
    rs.Close
    db.Close
     
    End Sub
    Okay this thing keeps giving me some crap about "you must save the field before you execute" for the setfocus part.

    How do I work with recordsets so I can assign the values from my query results to the other text fields??

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Each combo box has on On Change event. You can use it as follows:

    Private sub combo1_change()
    'Assuming combo2 is based on a query with combo1 in the where clause
    combo2.requery
    'Otherwise
    combo2.recordsource = "Select something from sometable where a field = """ & combo1.value & """ "
    combo2.requery

    End sub

  4. #4
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by RayMilhon View Post
    Each combo box has on On Change event. You can use it as follows:

    Private sub combo1_change()
    'Assuming combo2 is based on a query with combo1 in the where clause
    combo2.requery
    'Otherwise
    combo2.recordsource = "Select something from sometable where a field = """ & combo1.value & """ "
    combo2.requery

    End sub
    I understand what you're trying to tell me... but on my form I only have ONE combo box which will determine the contents of multiple TEXT boxes.

    In other words, selecting something from the combox runs a query to grab and fill out textboxes with data from the DB.

  5. #5
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    Ok, so let me ask a question the query that fills out the textboxes (If I'm reading this correctly) returns 1 record with multiple fields and the fields are assigned to different text boxes.

    Assuming that is correct then you need to add the following at line 14 in your code

    If rs.eof then
    textbox1 = ""
    textbox2 = ""
    Else
    textbox1 = rs!field1
    textbox2 = rs!field2
    End if

  6. #6
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    This silly thing vies me an error on the following line


    Code:
    cbx_UserID.SetFocus

    Run-time error '2108':

    You must save the field before you execute the GoToControl action, the GoToControl method, or the SetFocus method.


    WTF??

  7. #7
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    Here is ALL the code I have in my Code Builder


    Code:
    Option Compare Database
     
    Private Sub Form_Load()
    Dim strRowSource As String
    strRowSource = "SELECT DISTINCT [q_User_IDs].[User_ID] FROM q_User_IDs ORDER BY [User_ID]"
    cbx_UserID.RowSource = strRowSource
     
    End Sub
     
    Private Sub cbx_UserID_AfterUpdate(Cancel As Integer)
     
        If IsNull(Me.cbx_UserID) Then
             Me.cbx_UserID = Me.cbx_UserID.ItemData(0)
         End If
     
    Call updateTextFields
    End Sub
     
     
    Private Sub updateTextFields()
     
         Dim DescrSQL As String
         Dim db As DAO.Database
         Dim rs As DAO.Recordset
     
     
         DescrSQL = _
             "SELECT Duty_Pos " & _
             "FROM Surveys " & _
             "WHERE User_ID = " & cbx_UserID
     
     
        Set db = CurrentDb()
         Set rs = db.OpenRecordset(DescrSQL)
     
         With rs
              .MoveFirst
              txt_DutyPos = rs!field1
         End With
     
    End Sub
    And now, that code yields an Error "The expression On Load you entered as the event property produced the following error: Procedure Declaration Does Not Match Description of Event or procedure having the same name"

  8. #8
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,085
    First thing to do is under Option Compare Database put Option Explicit This requires you to dim any variables prior to use. Then do a compile. That should highlight the error and show you exactly which line it is.

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

Similar Threads

  1. expression builder
    By tonyl in forum Access
    Replies: 3
    Last Post: 11-26-2011, 12:00 AM
  2. Expression Builder Problem
    By benthamq in forum Forms
    Replies: 3
    Last Post: 10-07-2011, 01:02 PM
  3. Expression Builder or VB ... Help
    By Adynn in forum Access
    Replies: 0
    Last Post: 06-03-2011, 09:51 AM
  4. Expression builder
    By PJ_d_DJ in forum Access
    Replies: 2
    Last Post: 02-24-2011, 03:38 AM
  5. Expression Builder
    By mistaken_myst in forum Access
    Replies: 2
    Last Post: 05-07-2008, 01:30 PM

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