Results 1 to 4 of 4
  1. #1
    sweetiepie is offline Novice
    Windows 2K Access 2003
    Join Date
    Jun 2012
    Posts
    5

    Populate field

    I have a field txtSource that I would like to be populated based on what a user selects from the cboFrom field. TxtSource is an unbound field on the form.

    If "Home" is selected from cboFrom I would like for txtSource to be populated with the word home. Otherwise I would like txtSouce to equal what has been entered in txtOrigin (which is a bound field)

    What code to write or where to insert it....in the form or in the cboFrom combo box. Please help

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Place this code in the Code Module for your Form:

    Code:
    Private Sub cboFrom_AfterUpdate()
     If Me.cboFrom = "Home" Then
      Me.txtSource = "Home"
     Else
      Me.txtSource = Me.txtOrigin
     End If
    End Sub
    
    
    Private Sub txtOrigin_AfterUpdate()
     If Me.cboFrom = "Home" Then
      Me.txtSource = "Home"
     Else
      Me.txtSource = Me.txtOrigin
     End If
    End Sub
    
    
    Private Sub Form_Current()
     If Me.cboFrom = "Home" Then
      Me.txtSource = "Home"
     Else
      Me.txtSource = Me.txtOrigin
     End If
    End Sub
    Linq ;0)>

  3. #3
    sweetiepie is offline Novice
    Windows 2K Access 2003
    Join Date
    Jun 2012
    Posts
    5
    Do I need to place the code in all three places? Sorry I'm new to coding so I just wanted to be sure.

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Yes; the Values of both cboFrom and txtOrigin are involved here, and you have no way of knowing which will be populated first, or even if both will be populated, hence the code in both of these AfterUpdate events. And since txtSource is Unbound, you have to have code in the Form_Current event to do the evaluation each time you move to a given Record, in order for txtSource to be appropriate to that given Record.

    I should have noted before that this will only work if the Form in question is a Single View Form, i.e. one where you can only view one Record at a time. If it is a Continuous View or Datasheet View Form, another method will be needed to do this same thing. In that case you'd need to use the Control Source of the txtSource Textbox, and enter this in the Property box

    =IIf([cboFrom]="Home",[cboFrom],[txtOrigin])

    You could actually use this for any type of Form, but I tend to stay away from using the IIF() function, unless nothing else will work. IIF() can have unexpected results, at times, so personally I just avoid it. Your choice!

    Linq ;0)>

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

Similar Threads

  1. Auto Populate Txt Field
    By Andyjones in forum Forms
    Replies: 1
    Last Post: 03-27-2012, 05:53 PM
  2. populate unbound field
    By DCV0204 in forum Access
    Replies: 3
    Last Post: 03-16-2012, 02:41 PM
  3. Replies: 4
    Last Post: 01-30-2012, 08:32 AM
  4. Replies: 3
    Last Post: 10-03-2011, 02:33 PM
  5. Populate field from field on previous record
    By randolphoralph in forum Forms
    Replies: 7
    Last Post: 03-04-2011, 11:28 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