derek
derek

Reputation: 1055

VB open form on a specific record

I am using Microsoft access and i want to open a form to a specific id when a button is clicked. Can i specify the id in the Do.open form command. the code below opens a form but then a dialog box opens asking to enter the id . anyone any ideas ?

Private Sub button1_Enter()
Dim recordID As Integer
recordID = Me.CurrentRecord
DoCmd.OpenForm "Form3", , , "ID = recordID"
End sub

Upvotes: 4

Views: 60522

Answers (4)

John
John

Reputation: 1

Here's what has worked for me when I want to open a form (call it the Destination form) from another form (call it the Launch form) AND I want the records viewed in Destination form to be filtered (i.e., restricted) to only the record that was current in the Launch form AND the field that I want to use for filtering the records is of a text (or "string") data type:

Private Sub cmdOpenDestinationForm

DoCmd.OpenForm "frmDestination",,,"[FieldName]=" & "'" & Me.ControlName & "'"

End Sub

[FieldName] refers a field in the Destination form. [ControlName] refers to a control (like a text box) on the Launch form. The "Me" also refers to the Launch form, because that's the form that is open when you click the command button (and you could omit it). In effect, this code is telling Access to (1) open frmDestination, (2) Search through the "FieldName" field until you find a record that has the same value as that in Me.ControlName, and (3) Filter the Destination form so as to show only that one record.

The tricky part are all those double quotes ( " ), single quotes ( ' ) and ampersands (&). Note that [FieldName]= has opening and closing double quotes: "[FieldName]=". And don't forget the = symbol. Next, this is followed by & "'". That's an ampersand, a double quote, a single quote, and another double quote. In my experience, you cannot have spaces between the quote symbols. They must be run together as shown. Then this is followed by another ampersand and the name of the control on the Launch form that has the value you want to locate in [FieldName] on the Destination form: & Me.ControlName. And, as stated before, you can omit the Me if you want. I use it because helps me remember that ControlName is a control on the Launch form, not the Destination form. Then this is followed by yet another & and another set of the double and single quotes: "'".

Upvotes: 0

user11728143
user11728143

Reputation: 1

How about this solution?

Private Sub Command10_Click()
On Error GoTo Err_Command10_Click
    Dim IDnr As String
    Dim stDocName As String
    Dim stLinkCriteria As String
    IDnr = InputBox("Enter ID")
    stDocName = "FORM_MASTER"
    stLinkCriteria = "[ID]=" & IDnr
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Exit_Command10_Click:
    Exit Sub
Err_Command10_Click:
    MsgBox Err.Description
    Resume Exit_Command10_Click
End Sub

Upvotes: 0

Laurent
Laurent

Reputation: 11

I tried I had the same, be sure that the name you give to the id is the name used in the form, not in the DB! IE, my id is id in DB, but pc_id in my form!

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91316

First, change:

recordID = Me.CurrentRecord

To:

recordID = Me.ID

Where Me.ID is the name of a field or control on the current form ( Microsoft Access - get record id when button is clicked ).

When you refer to a variable, put it outside the quotes:

DoCmd.OpenForm "Form3", , , "ID = " & recordID

This is going to be fine for an ID, which is numeric, but it will get a little more complicated with text and dates, because you will need delimiters. This will work well as long as sometextvar does not contain a quote:

DoCmd.OpenForm "Form3", , , "Atext = '" & sometextvar & "'"

Otherwise

DoCmd.OpenForm "Form3", , , "Atext = '" & Replace(sometextvar,"'","''") & "'"

And dates take #

DoCmd.OpenForm "Form3", , , "ADate = #" & somedatevar & "#"

To avoid problems with locales, it is nearly always best to format to year, month, day, so:

DoCmd.OpenForm "Form3", , , "ADate = #" & Format(somedatevar,"yyyy/mm/dd") & "#"

Upvotes: 7

Related Questions