On Open Set Field Value

C

ChrisG

How can I set a field value when a user opens form?

I want to set ShpQteType = "Pullbox" when a user opens form SHPQTE PB
Form uses a Query (ShpQte Query) to enter new data
I tried to do this using the Expression builder =[SHPQTE]![ShipQteType] =
"Pullbox" But I got an error.
Also Tried Code builder and it didn't set the value in the table.
Private Sub Form_Open(Cancel As Integer)
Dim shipQteType As String
shipQteType = "Pullbox"
End Sub
 
K

Klatuu

Use the Default Value property of the control bound to the field you want to
populate. Set it in form design view using the properties dialog box.

Also, when you want to address controls on a form you are opening, use the
Load event rather than the Open event. The Open event may be too soon and
the control references may not yet be established. The Open event is better
used when you need to make a decision on whether to allow the form to open or
cancel the open using Cance = True.
 
G

George Nicholson

Try setting it in the Form_Current event:

If Me.NewRecord Then
shipQteType = "Pullbox"
End If

Even if your form is only used to add new records, it's a little safer.
 
K

Klatuu

Safer than using the control's Default Value property?
How so?
--
Dave Hargis, Microsoft Access MVP


George Nicholson said:
Try setting it in the Form_Current event:

If Me.NewRecord Then
shipQteType = "Pullbox"
End If

Even if your form is only used to add new records, it's a little safer.
--
HTH,
George


ChrisG said:
How can I set a field value when a user opens form?

I want to set ShpQteType = "Pullbox" when a user opens form SHPQTE PB
Form uses a Query (ShpQte Query) to enter new data
I tried to do this using the Expression builder =[SHPQTE]![ShipQteType] =
"Pullbox" But I got an error.
Also Tried Code builder and it didn't set the value in the table.
Private Sub Form_Open(Cancel As Integer)
Dim shipQteType As String
shipQteType = "Pullbox"
End Sub
 
C

ChrisG

I agree normally I would use control's Default Value property, but I negected
to tell you there are two values needed and depending on which form is used
is a value assigned. Thank you both for your prompt answers and teaching me
a little bit more about Access.
--
ChrisG


Klatuu said:
Safer than using the control's Default Value property?
How so?
--
Dave Hargis, Microsoft Access MVP


George Nicholson said:
Try setting it in the Form_Current event:

If Me.NewRecord Then
shipQteType = "Pullbox"
End If

Even if your form is only used to add new records, it's a little safer.
--
HTH,
George


ChrisG said:
How can I set a field value when a user opens form?

I want to set ShpQteType = "Pullbox" when a user opens form SHPQTE PB
Form uses a Query (ShpQte Query) to enter new data
I tried to do this using the Expression builder =[SHPQTE]![ShipQteType] =
"Pullbox" But I got an error.
Also Tried Code builder and it didn't set the value in the table.
Private Sub Form_Open(Cancel As Integer)
Dim shipQteType As String
shipQteType = "Pullbox"
End Sub
 
G

George Nicholson

Dave, my "safer" comment wasn't meant in response to your post (that hadn't
appeared yet).

I was simply trying to indicate that it would be safer to *verify* that you
were dealing with a new record rather than *assume* you were, even when
opening a form in Data entry mode, since that is soooo easy to change
somewhere down the line. Of course, setting DefaultValue as you suggested
accomplishes the same thing since it is only applied to NewRecords.

--
HTH,
George


Klatuu said:
Safer than using the control's Default Value property?
How so?
--
Dave Hargis, Microsoft Access MVP


George Nicholson said:
Try setting it in the Form_Current event:

If Me.NewRecord Then
shipQteType = "Pullbox"
End If

Even if your form is only used to add new records, it's a little safer.
--
HTH,
George


ChrisG said:
How can I set a field value when a user opens form?

I want to set ShpQteType = "Pullbox" when a user opens form SHPQTE PB
Form uses a Query (ShpQte Query) to enter new data
I tried to do this using the Expression builder
=[SHPQTE]![ShipQteType] =
"Pullbox" But I got an error.
Also Tried Code builder and it didn't set the value in the table.
Private Sub Form_Open(Cancel As Integer)
Dim shipQteType As String
shipQteType = "Pullbox"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top