Opening up a form where it puts the ID in automatically?

yepwingtim

Registered User.
Local time
Today, 13:00
Joined
Jun 6, 2008
Messages
126
I have a form that is only a screenshot.

I want to have a form button where once you click on it,
we add a new record in that matches the ID to ID,
and puts in the ID for you here what I have so far.

Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub
________
BUY DIGITAL SCALE
 
Last edited:
I have a form that is only a screenshot.

I want to have a form button where once you click on it,
we add a new record in that matches the ID to ID,
and puts in the ID for you here what I have so far.

Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub

I don't think you will be about to do this using the Link Criteria parameter.

I usually try to handle this with a sub form. This way there is not VBA code needed.

When a sub form is not possible, then I use one of these two methods:

1) Push the value using the DoCmd.OpenForm parameter for OpenArgs
or
2) Pullthe value form the other form using a form reference

Note: With both methods, you will use the newly opened form's On Current event to set the value into a control from the other form.
 
Assuming that you want to create a new record with the passed ID value and use the Push method using the OpenArgs:

Code:
Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"


DoCmd.OpenForm stDocName, , , , acFormAdd, , Me![ID]
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub


The in the form "history Query" in the On Current Event you could use something like this:

Code:
Private Sub [B]Form_Current[/B]()
   Me.ID = Me.OpenArgs
End Sub
 
Last edited:
Assuming that you want to create a new record with the passed ID value and use the Pull method:

Code:
Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"


DoCmd.OpenForm stDocName, , , , acFormAdd
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub


The in the form "history Query" in the On Current Event you could use something like this:

Code:
Private Sub [B]Form_Current[/B]()
   Me.ID = forms![YourOtherForm].ID
End Sub
 
Last edited:
Hi HiTechCoach, thanks for helping me again

This is what I have so far

Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , , , , "Add"
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub

When I open the Form I use

Private Sub Form_Open(Cancel As Integer)
If Me.OpenArgs = "Add" Then
ID.DefaultValue = [Forms]![find a patient]![ID]
End If
End Sub

The problem is when I go back to the tab, and push the button to go to add another record, the ID will stay the same unless I close that tab. Anyway to resolve this?
________
HERBAL VAPE
 
Last edited:
Hi HiTechCoach, thanks for helping me again

This is what I have so far

Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "history Query"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , , , , "Add"
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click

End Sub

When I open the Form I use

Private Sub Form_Open(Cancel As Integer)
If Me.OpenArgs = "Add" Then
ID.DefaultValue = [Forms]![find a patient]![ID]
End If
End Sub

The problem is when I go back to the tab, and push the button to go to add another record, the ID will stay the same unless I close that tab. Anyway to resolve this?

IMHO, this code is not is the correct event.

Code:
Private Sub [B]Form_Open[/B](Cancel As Integer)
If Me.OpenArgs = "Add" Then
ID.DefaultValue = [Forms]![find a patient]![ID]
End If
End Sub


I would move your code to the On Current event. If you will table another look at my examples, you will see that is how I did it.
 

Users who are viewing this thread

Back
Top Bottom