Default value in form

  • Thread starter Thread starter MET
  • Start date Start date
M

MET

I am attempting to populate some of the fields when
adding data to my tables. Please bear with me, as I am a
complete novice at this. My first call was through this
subroutine. This part appears to be functioning properly.

Private Sub Race_Exit(Cancel As Integer)
Dim varTrack As String
If (IsNull([Track])) Then
varTrack = LastTrack()
End If
End Sub

Here comes my problem. I am wanting to access the current
form, which called the above subroutine, and display the
field [Track] from the last record of the table. This
data can repeat for a number of records, enough so that I
must make this a data item. The current table is
tblResults.

Private Function LastTrack() As String
Dim rst As Recordset
Dim LastRaceTrack As String
Set rst = CurrentDb.OpenRecordset("tblResults",
dbOpenDynaset)

rst.MoveLast
LastRaceTrack = Me!Track

LastTrack = LastRaceTrack
End Function

Thanks for taking a look at this.
 
Here are a couple of ideas:

1.
One way to do it is to create two text boxes in the form Header named
txtDate and txtTestNo.
Leave the control source blank - this means they will be unbound text boxes.
In the Form Open event you can set their initial values.
Something like:
Me![txtDate] = Date()
Me![txtTestNo] = DMax("Test Number","Test") +1

When the form is open, you can change the values to be whatever you really
want them to be.

In the Before Insert event (i.e. when a new record is added) use:
Me![Test Number] = Me![txtTestNo]
Me![TestDate] = Me![txtDate]

This way the user never has to enter those values, the code fills them in
automatically.

2. A simple solution for each field is to use the After_Update event to
modify the default value.
This way, when a new record is created, the field defaults to the previously
entered value.

Private Sub txtName_AfterUpdate()
'That's 4 double quotes on either side!
Me![txtName].DefaultValue = """" & Me![txtName].Value & """"
End Sub
 
Thanks Joe for the help.
-----Original Message-----
Here are a couple of ideas:

1.
One way to do it is to create two text boxes in the form Header named
txtDate and txtTestNo.
Leave the control source blank - this means they will be unbound text boxes.
In the Form Open event you can set their initial values.
Something like:
Me![txtDate] = Date()
Me![txtTestNo] = DMax("Test Number","Test") +1

When the form is open, you can change the values to be whatever you really
want them to be.

In the Before Insert event (i.e. when a new record is added) use:
Me![Test Number] = Me![txtTestNo]
Me![TestDate] = Me![txtDate]

This way the user never has to enter those values, the code fills them in
automatically.

2. A simple solution for each field is to use the After_Update event to
modify the default value.
This way, when a new record is created, the field defaults to the previously
entered value.

Private Sub txtName_AfterUpdate()
'That's 4 double quotes on either side!
Me![txtName].DefaultValue = """" & Me![txtName].Value & """"
End Sub

--
Joe Fallon
Access MVP



I am attempting to populate some of the fields when
adding data to my tables. Please bear with me, as I am a
complete novice at this. My first call was through this
subroutine. This part appears to be functioning properly.

Private Sub Race_Exit(Cancel As Integer)
Dim varTrack As String
If (IsNull([Track])) Then
varTrack = LastTrack()
End If
End Sub

Here comes my problem. I am wanting to access the current
form, which called the above subroutine, and display the
field [Track] from the last record of the table. This
data can repeat for a number of records, enough so that I
must make this a data item. The current table is
tblResults.

Private Function LastTrack() As String
Dim rst As Recordset
Dim LastRaceTrack As String
Set rst = CurrentDb.OpenRecordset("tblResults",
dbOpenDynaset)

rst.MoveLast
LastRaceTrack = Me!Track

LastTrack = LastRaceTrack
End Function

Thanks for taking a look at this.


.
 
Back
Top