Results 1 to 6 of 6
  1. #1
    Mattress58 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5

    Combo Box Polulate Text Box

    Below is the example. I Have a combo Box and when I select it, I need a text box to populate an answer based on a lookup in the access table. Here is what I have.



    Two things here I need help with:
    A. This code below works the first time only. After I select a MARKET it populates TxtRes, but if I select a different MARKET, the answer doesnt change.
    B. Here is have it looking at TxtMO textbox to get the value for MONTH (which is a number and works great)

    Code:
     " & "MONTH = " & Str(Me.TxtMO.Value) & "
    How can I change

    Code:
    MARKET = 'Alberta'
    to be in this same format. I need the ' ' around it but cant seem to get it to work.


    I think if I can get question B above to work it may fix question A and B.



    Code:
    Private Sub COStation_AfterUpdate()
    
        Dim dbTemp As Database
        Dim rsTemp As DAO.Recordset
        Set dbTemp = CurrentDb()
    
    
        Set rsTemp = dbTemp.OpenRecordset("SELECT Net FROM His WHERE (MARKET = 'Alberta' And Segment = 'Combined' AND " & "MONTH = " & Str(Me.TxtMO.Value) & " And " & "YEAR = " & Str(Me.TxtYR.Value) & ")")
    
    
    
    
        If rsTemp.EOF = False Then Me.TxtRes.Value = rsTemp("Net")
        If rsTemp.EOF = True Then Me.TxtRes.Value = ""
    
    
       rsTemp.Close
       Set rsTemp = Nothing
    
    
    End Sub
    When I select Alberta, TxtRes should populate with -20. I am trying to get the Net value for the combo box selected with TxtMO, TxtYR, and "Combined" filtered down to get that answer


    Thank you for any help. Ive been trying to get this to work for a couple days now.
    Attached Thumbnails Attached Thumbnails example Access.jpg  

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    This
    " & "MONTH = " & Str(Me.TxtMO.Value) & "

    Would get changed to this in order to manage literal text.
    " & "MARKET = '" & Me.ControlName.Value & "' And

    I use the Immediate window to see if my variables are getting concatenated correctly. Debug.Print before you execute the SQL. You can open the Immediate Window within the VBA editor and view the results.

    Dim strSQL as string

    strSQL = "My text string" & Me.txt & "more text"
    debug.print = strSQL
    Last edited by ItsMe; 06-16-2015 at 07:33 AM. Reason: Corrected Syntax

  3. #3
    Mattress58 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    For some reason I keep getting Run Time error 13 type mismatched. When I debug and hold the cursor over that line:

    Code:
    " & "MARKET = '" & Str(Me.COStation.Value) & "'
    It shows the current market that I have selected like "Alberta". I assume everything else is correct because it works when I have it as

    MARKET = 'Alberta'
    Any thoughts? Thanks again for the help

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Oh, sorry. Not sure why I included the Str function. There is not usually a need for casting within Access and my example is a misuse of the function.


    " & "MARKET = '" & Me.COStation.Value & "' And

  5. #5
    Mattress58 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    AWWWWWW. That worked. Thank you so much.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You bet ...

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

Similar Threads

  1. Replies: 1
    Last Post: 07-22-2014, 01:24 PM
  2. Replies: 2
    Last Post: 08-16-2012, 10:02 PM
  3. Replies: 3
    Last Post: 03-15-2012, 02:27 PM
  4. Replies: 4
    Last Post: 08-16-2011, 05:54 PM
  5. Combo box to text box
    By kc21supra in forum Access
    Replies: 4
    Last Post: 08-21-2009, 01:08 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