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
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") & "#"
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!
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
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: "'".
Related
I have two forms: transfert Form with its subform and intransfert Form. I am using
DoCmd.OpenForm "intransfert", , , , acFormAdd, acDialog, Me!Text83
(where text83 is =[transfertasubform].[Form]![transfertadetailid] under
Private Sub Command78_Click()
in transfet form and
Private Sub Form_Load()
On Error Resume Next
If Me.NewRecord Then Me!trnrin = Me.OpenArgs
in intransfet form. intransfert form is based in transfertdetailquery. i wont to prevent passing text83 value more then one time
i tried to explain my problem and expect a help to prevent duplicates when used Arge
assuming trnrin is the name of a variable in your record source. assuming you mean that you want to avoid adding two records where trnrin has the same value and the user sent the same open args twice. assuming trnrin is also the name of a control in the detail section of the intransfert form.
'form load only runs when the form is opened so you have to close the form to pass new args
'but you can just move the same code to a button or whatever
Private Sub IntransferForm_Load()
If Len(Me.OpenArgs) > 0 Then
Me.txtBoxintheHeader = Me.OpenArgs 'the load event is the right place to set controls
'most of this code is to check if there is already an instance where trnrin = the OpenArgs in the record source
Dim lookedupArgs As String
lookedupArgs = Nz(lookedupArgs = DLookup("trnrin", "MyTable", "trnrin = " & "'" & Me.OpenArgs & "'"), "ValuethatisneveranArg")
If lookedupArgs = "ValuethatisneveranArg" Then
'Debug.Print "trnrin = '" & Me.OpenArgs & "'" 'note the string delimiters
'Me.trnrin = Me.OpenArgs 'this surprisingly works but will break easily and doesn't handle complex cases
Dim rs As Recordset
Set rs = Me.Recordset 'if recordset is bound to a table then table will also be updated.
'you can also bind to the table directly but you may need to call me.requery to show the changes
rs.AddNew
rs!trnrin = Me.OpenArgs
rs.Update
End If
End If
End Sub
I am looking to try and have a text box that i type in a number and i want it to open another form based on that number. So i have form frmNonMgr that i type a number in the text field txtPhone and i want it to open frmFolder_Tabs to the record that relates to that number. [Phone] in the employees table are unique and you cannot have two of the same, when ive gotten it to work its just opening the last record or a blank record.
Private Sub btnFolder_Click()
If IsNull(Me.txtPhone) Then
MsgBox "Please enter a Phone"
Me.txtPhone.SetFocus
Else
DoCmd.OpenForm "frmFolder_Tabs", , , "[Phone]='" & Me.txtPhone
DoCmd.Close acForm, "frmNonMgr"
End If
End Sub
It would be better to store phone number to a variable and then close the active form first. Then open your target form and use filter with stored variable. Also for string type data you need to enclose value with single quote what Kostas already mentioned. Try below codes.
Private Sub btnFolder_Click()
Dim strPhone As String
If IsNull(Me.txtPhone) Then
MsgBox "Please enter a Phone"
Me.txtPhone.SetFocus
Else
strPhone = Me.txtPhone
DoCmd.Close
DoCmd.OpenForm "frmFolder_Tabs", , , "[Phone]='" & strPhone & "'"
End If
End Sub
I just started with Access not too long ago and I'm having some trouble.
I am trying to filter a report that displays tracking numbers and associated data scanned in from barcodes on boxes. The tracking number is the primary key and it is stored as Short Text in the table.
I have a command button set up in a form that launches an input box to type or scan in the tracking number.
Whenever try to run this I get a
Run-time error '3464: Data type mismatch in the criteria expression.
My code:
Private Sub Command28_Click()
Dim ReportName As String
ReportName = "bucketContents"
Dim trackingNum As String
trackingNum = InputBox("Enter the Tracking number:", "Tracking Number Input")
'if a value was entered, open and filter report
If Len(Trim(trackingNum)) > 0 Then
DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = " & trackingNum
End If
End Sub
Text values must be wrapped in quotes:
DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = '" & trackingNum & "'"
I am troubleshooting and found that a button
does not seem to be working.
I want the button to print a report (from rptHerstelfiche) with the "IDDefect" as "Volgnr" (in rptHerstelfiche).
The code linked to that button.
Option Compare Database
Option Explicit
Private Sub Knop164_Click()
On Error GoTo Err_Afdrukken_technische_gegevens_Click
Dim stDocName As String
DoCmd.RunCommand acCmdSelectRecord
stDocName = "rptHerstelfiche"
'DoCmd.OpenReport "report name", acViewPreview,
DoCmd.OpenReport stDocName, acPreview, , "Volgnr=" & Me.IDDefect
Exit_Afdrukken_technische_gegevens_Click:
Exit Sub
Err_Afdrukken_technische_gegevens_Click:
MsgBox Err.Description
Resume Exit_Afdrukken_technische_gegevens_Click
End Sub
What you have looks good, but a few things:
Because the current reocrd is being sent to the report? Then the current reocrd may NOT yet have been saved. So what you see in the report will not show what you have in the form.
I suggest this code. and the "select" record is not required.
Dim stDocName As String
if me.Dirty = True then me.Dirty = False ' force data save
stDocName = "rptHerstelfiche"
DoCmd.OpenReport stDocName, acPreview, , "Volgnr=" & Me.IDDefect
The last line looks a bit "suspect", and should read like this:
DoCmd.OpenReport stDocName, acViewPreview, , "Volgnr=" & Me.IDDefect
Note the use of acViewPreview. And the editor SHOULD have suggested this contant.
Also, if Volgnr a number type column, or a string/text? You have to check this in the table designer.
If Volgnr is a text data type, then that line of code has to become this:
DoCmd.OpenReport stDocName, acViewPreview, , "Volgnr = '" & Me.IDDefect & "'"
note the requirement to surround the value with quotes.
So in summary:
check and use acViewPreview
force the record to be written to the table before you launch report
if you don't' write the record, then the report will show values not yet updated
check if the data type is a number/long/integer type or a text column
if a text column, then surround the value with quotes.
My question is sounds simple:
I want to print some query using button in ms access form.
Example:
Private Sub Print_Click()
sqls = " SELECT * FROM " _
& "NAME WHERE ID = " & variable & ";"
Print sqls on paper ' This is just a simple explanation of what I want
Design a report from table. Use report Record Source= YourTable. In form use a Command Button to print report. The button will filter report data based on your variable and print report through default printer. Here "[ID]=" & variable this is Where Condition of command. This part will filter data as variable.
Private Sub Print_Click()
'DoCmd.OpenReport "YourReportName", acPreview, , "[ID]=" & Me.txtID
DoCmd.OpenReport "YourReportName", acNormal, , "[ID]=" & variable
End Sub
On a form, any form, put an unbound Text Box. In my example below I have named the text Box 'XportXL'. Create a command button. Enter the name of your query in the unbound control. This will work for any query - just enter its name in the unbound control. ("E:\Peter\Desktop\filename.xlsx" is just the place where I wnat to expert to) put the following code in the OnClick event:
Dim MyQuery As String
MyQuery = Me.XportXL
If IsNull(Me.XportXL) Then
DisplayMessage ("We need something to export.")
Exit Sub
Else
DoCmd.OutputTo acOutputQuery, MyQuery, acFormatXLSX, "E:\Peter\Desktop\filename.xlsx", False, "", , acExportQualityScreen
End If