Opening form with DoCmd.OpenForm - vba

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.

Related

Open form based on text value

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

Syntax error in my If Else statement in my VBA access code

I have been working on this problem for a while and I'm close to the answer thanks to the help of stack overflow and google. Let me give you some insight. I have a table and 2 forms and a Key named CalibrationKey that auto increments as a Autonumber to keep track of the record and how many and what not. On 1 form are all my table fields. On my 2nd form is the Subfields to the first form that feed into the table. First form is Calibration and 2nd form is Calibration Detail. In my first form, I have it so you can edit OR add a new record(where my problem started). My 2nd form I have it so you can only add a new record and have the Data Entry on Property Sheet to YES as I only want you to be able to add a new record from that form. So I made a If Else statement as a click event in my First Form that if my Text box is Empty, then you add a new record. If it is NOT empty, then you edit the form. But, I get a Syntax error every time I try and do this. I'm not sure what the problem is.
Private Sub Make_Click()
If Not IsNull(Make.Value) Then
DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormAdd, acDialog
Else: DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormEdit, acDialog
End If
End Sub
Then I found that a IsEmpty might be easier, but it gives the same Syntax error in the ELSE line of code as the Not IsNull line did.
Private Sub Make_Click()
If IsEmpty(Make.Value) Then
DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormAdd, acDialog
Else: DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], acFormEdit, acDialog
End If
End Sub
Trying to diagnose the problem of the incorrect Syntax as Either IsEmpty or Not IsNull seems to give the same syntax error on the same line. Looking to correct the syntax error and which is function is better to use, rather it be IsEmpty, or Not IsNull
Try using the normal If-Then-Else syntax:
Private Sub Make_Click()
Dim DataMode As acFormOpenDataMode
If Me.Dirty = True Then
'Save record.
Me.Dirty = False
End If
If Not IsNull(Make.Value) Then
DataMode = acFormOpenDataMode.acFormAdd
Else
DataMode = acFormOpenDataMode.acFormEdit
End If
DoCmd.OpenForm "Calibration Detail", , , "[CalibrationKey]= " & Me.[CalibrationKey], DataMode, acDialog
End Sub

Printing records on paper using ms access buttons

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

Print Dialog in MS-Access

I want to open a print dialog box. I haven't programmed in VB for a decade and I am more than a little rusty.
I got a copy of an MS Access VB script that selects an MDB file and then prints only one copy. My thought process was:
Open up a dialog box;
Select the printer;
Type in number of copies;
Print.
Currently, the portion of the original script I want to modify is:
[SQL query above this]
....
' Open form
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("select##identity")
Debug.Print rs(0)
Dim q As String
q = "transfer_id=" & rs(0)
DoCmd.OpenForm "Transfer Report", acNormal, , q, ,acDialog
DoCmd.PrintOut
...
[End If, End Sub, etc.]
This only prints out one instance of the report and the company does not have the ability to print out other copies at the same time. Since it is in the field, they don't have access to a copier but still need multiple copies.
One answer I found, by David-W-Fenton (thank you!), shows how to create a dialog box; Would the following script accomplish what I want to do? And, how do I add a portion to the dialog box to specify how many copies to print?
...
Dim varPrinter As Printer
Dim strRowsource As String
Dim q As String
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("select ##identity")
Debug.Print rs(0)
If Len(Me.OpenArgs) > 0 Then
q = Me.OpenArgs
Me.Tag = q
For Each varPrinter In Application.Printers
strRowsource = strRowsource & "; " & varPrinter.DeviceName
Next varPrinter
Me!cmbPrinter.RowSource = Mid(strRowsource, 3)
' first check to see that the report is still open
If (1 = SysCmd(acSysCmdGetObjectState, acReport, q)) Then
With Reports(q).Printer
Me!cmbPrinter = .DeviceName
Me!optLayout = .Orientation
End With
Me!txtPageTo = Reports(q).Pages
End If
End If
Public Function PrintReport(q As String) As Boolean
q = "transfer_id=" & rs(0)
' open report in PREVIEW mode but HIDDEN
DoCmd.OpenReport q, acViewPreview, , , acHidden
' open the dialog form to let the user choose printing options
DoCmd.OpenForm "dlgPrinter", , , , , acDialog, q
With Forms!dlgPrinter
If .Tag <> "Cancel" Then
Set Reports(q).Printer = Application.Printers((!cmbPrinter))
Reports(q).Printer.Orientation = !optLayout
Application.Echo False
DoCmd.SelectObject acReport, q
DoCmd.PrintOut acPages, !txtPageFrom, !txtPageTo
PrintReport = True
End If
End With
DoCmd.Close acForm, "dlgPrinter"
DoCmd.Close acReport, q
Application.Echo True
End Function
Here is a late reply, some times it may help others.
In order to open the print dialogue:
Place a command button for PRINT on the report, and write the code as below.
Private Sub CmdbtnPrint_Click()
DoCmd.RunCommand acCmdPrint
End Sub
Change the 'Display When' property of button to 'Screen Only'.
Open the report in 'acViewReport' view mode.
Click the PRINT command button, and the systems print dialogue box will be open. Set the parameters and print the report.
Check the documentation about the PrintOut method:
http://msdn.microsoft.com/en-us/library/office/ff192667.aspx
In your original script to modify, just add the Copies argument to the DoCmd.PrintOut statement, like:
DoCmd.PrintOut Copies:=x
where x is a variable representing the number of copies you want to print.
Reviewing the documentation for the DoCmd.OpenForm method,
http://msdn.microsoft.com/en-us/library/office/ff820845.aspx
I don't think the code you have can allow a user-input for the number of copies. I do not program in Access, but I'm certain there are other ways to allow user input at run-time. In Excel/PowerPoint we would use an InputBox or a UserForm.
I think you can use an InputBox in Access:
http://office.microsoft.com/en-us/access-help/inputbox-function-HA001228856.aspx

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
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: "'".