Results 1 to 10 of 10
  1. #1
    Mile129 is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    11

    autopopulate based on two characters in a field

    Can I autopopulate another field in my form based on 2 characters in a another field.



    I have a field with a number, i.e. 11275, in the field below it I want to lookup the first two number, "11" and then find the corresponding data it relates to in a table I have, i.e. 11 = MJ.

    So in field one someone enters 11275 and then in in the field below it, it automatically enters MJ. Please advise. Thanks.

  2. #2
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    To get the left two character from a string use the left function.

    Example:
    Code:
    print Left("11275",2)
    11
    You could then use Dlookup() to get the value.

    I owudl use the After Update event to run the code.
    Boyd Trimmell aka Hitechcoach
    Database Architect and Problem Solver
    Microsoft MVP - Access Expert
    25+ years specializing in Accounting, Inventory, and CRM systems
    "If technology doesn't work for people, then it doesn't work."

  3. #3
    Mile129 is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    11
    I'm sorry I'm fairly new to Access, can you please be more specific.

    I can put the code in as follows:

    Private Sub VoyageNo_AfterUpdate()
    print Left("what is needed here, Field 1?", 2)
    Dlookup("previous result?", "Table name")???
    Place where, next field?

    End Sub

    please advise, thanks.

  4. #4
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    Since we no nothing about your database, tbales, fields, etc. ity is very difficult to give you anything specific to your databse.

    Note: Fields are in tbales. Controls are on forms/reports. Controls can be bound to a field in the form's/report's record source.

    It might be easier to explain if you will to post a sample of your database.
    Boyd Trimmell aka Hitechcoach
    Database Architect and Problem Solver
    Microsoft MVP - Access Expert
    25+ years specializing in Accounting, Inventory, and CRM systems
    "If technology doesn't work for people, then it doesn't work."

  5. #5
    Mile129 is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    11
    Okay, sorry I'm not detailed enough, what I want is on my form, when someone enters in a "control" in the form called VoyageNo it updates the table's "field" with the same name. Now, in the control right below VoyageNo is a control called Ship, which currently is a dropdown and they select the ship. What I want is to be able to automatically populate the Ship Control from a table called shipcodes. The first two numbers determine the ship, so in the Shipcodes table it can find ship MJ when you put 11. Is this possible. I have attached the database, it's very simple.

  6. #6
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    See if the attach is what you want.

    Code:
    Private Sub VoyageNo_AfterUpdate()
    
    Dim varShipNum As Variant
    
    If Len(Me.VoyageNo) > 2 Then
    
      varShipNum = DLookup("[Ship]", "tblShipCodes", "[Number]=" & Val(Left(Me.VoyageNo, 2)))
    
      If IsNull(Me.cboShip) Then
         Me.cboShip = varShipNum
       
      Else
        
        If Me.cboShip <> varShipNum Then
        
          If MsgBox("Update Ship from " & Me.cboShip & " to " & varShipNum & "?", vbYesNo, "Confirm update ") = vbYes Then
             Me.cboShip = varShipNum
          End If
    
        End If
      End If
    
    End If
    
    End Sub
    Boyd Trimmell aka Hitechcoach
    Database Architect and Problem Solver
    Microsoft MVP - Access Expert
    25+ years specializing in Accounting, Inventory, and CRM systems
    "If technology doesn't work for people, then it doesn't work."

  7. #7
    Mile129 is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    11
    Thanks HiTech, seems like the macro is erroring out at:

    If IsNull(Me.cboShip) Then
    Me.cboShip = varShipNum

    I just want the ship control to be autopopulated based on the Voyage Number entry, first two characters of the Voyage number dictate the Ship.

    Why can't this be as easy as excel and simple vlookup?!?! j/k, I know access is a whole other beast. please help if you can, thanks.

    Oh and when I open the attached database you sent, it's telling me the following: "Unrecognized database format", I'm using Access 2003, if that helps.

  8. #8
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    Quote Originally Posted by Mile129 View Post
    Thanks HiTech, seems like the macro is erroring out at:

    If IsNull(Me.cboShip) Then
    Me.cboShip = varShipNum

    I just want the ship control to be autopopulated based on the Voyage Number entry, first two characters of the Voyage number dictate the Ship.

    Why can't this be as easy as excel and simple vlookup?!?! j/k, I know access is a whole other beast. please help if you can, thanks.

    Oh and when I open the attached database you sent, it's telling me the following: "Unrecognized database format", I'm using Access 2003, if that helps.

    What is the error?

    Did you notice that I also renamed the combo box for the Ship to use better naming.


    I have reattached the sample I did in a ZIP
    Boyd Trimmell aka Hitechcoach
    Database Architect and Problem Solver
    Microsoft MVP - Access Expert
    25+ years specializing in Accounting, Inventory, and CRM systems
    "If technology doesn't work for people, then it doesn't work."

  9. #9
    Mile129 is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    11
    Quote Originally Posted by HiTechCoach View Post
    What is the error?

    Did you notice that I also renamed the combo box for the Ship to use better naming.


    I have reattached the sample I did in a ZIP

    That's why, thanks HiTech! it works!

  10. #10
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    705
    You're welcome.

    Glad that worked for you.
    Boyd Trimmell aka Hitechcoach
    Database Architect and Problem Solver
    Microsoft MVP - Access Expert
    25+ years specializing in Accounting, Inventory, and CRM systems
    "If technology doesn't work for people, then it doesn't work."

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

Similar Threads

  1. Replies: 3
    Last Post: 05-26-2011, 12:52 PM
  2. MDB database field limited to 64 or 255 characters
    By galapogos in forum Programming
    Replies: 1
    Last Post: 04-06-2010, 10:22 AM
  3. Counting Characters in a text field
    By velvettiger in forum Queries
    Replies: 1
    Last Post: 03-12-2010, 12:36 PM
  4. Setting a field to only accept text characters, not numbers
    By USAFA2012 in forum Database Design
    Replies: 2
    Last Post: 03-09-2010, 12:37 PM
  5. Replies: 3
    Last Post: 12-15-2009, 01:47 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