Access locking up after first DoCmd.SendOjbect acSendReport - ms-access

I can click once to launch the following Event Procedure, but if I return to the form to click twice, Access locks up to the point I have to kill the task in TaskManager?
Any advice on this one?
Private Sub Command372_Click()
On Error GoTo ErrorHandler
gTroop = Str(Me.Troop)
DoCmd.SendObject acSendReport, "GirlScoutandTroopMatchGlobalVariable", "PDF", Me.TroopLeaderEmailAddress
MsgBox "Message Sent Successfully."
Cleanup:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501
MsgBox "Email message was Cancelled."
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume Cleanup
End Sub

Related

Convert often repeated code into a module

I am using the following VBA code:
Private Sub btnStatistics_click()
On Error GoTo Err_Handler
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
Exit Sub
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub
I am using this VBA code in many buttons in different forms to do different things. I want to move the part strPasswd = Format(Now, "Hh") * 2 into a module so I can update/change it in one place.
If it is only the test of the password that you want to move, create a Function that returns a Boolean:
Function PasswordOK(strPwd As String) As Boolean
PasswordOK = strPwd = Format(Now, "Hh") * 2
End Function
and then you can use it as:
If PasswordOK(strPasswd) Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
'Exit Sub '<-- this isn't needed, because the next
' statement after this one is also Exit Sub
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
Or, if appropriate, you could move even more of the code into the common routine by passing a few more parameters:
Sub ChangeForm(oldForm As String, newForm As String)
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
DoCmd.Close acForm, oldForm, acSaveYes
DoCmd.OpenForm newForm, acNormal, "", "", acEdit, acNormal
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
End If
End Sub
and use it as
Private Sub btnStatistics_click()
ChangeForm "frmCustomer", "frmStatistics"
End Sub
Or perhaps somewhere between the two, putting just the input of the password, and its testing, into the common routine:
Function PasswordOK() As Boolean
Dim strPasswd As String
strPasswd = InputBox("Please Enter Password", "Password Required")
If strPasswd = Format(Now, "Hh") * 2 Then
PasswordOK = True
Else
MsgBox "Incorrect password!", vbOKOnly, "Password Info"
PasswordOK = False
End If
End Function
and use it as
Private Sub btnStatistics_click()
On Error GoTo Err_Handler
If PasswordOK() Then
DoCmd.Close acForm, "frmCustomer", acSaveYes
DoCmd.OpenForm "frmStatistics", acNormal, "", "", acEdit, acNormal
End If
Exit_This_Sub:
Exit Sub
Err_Handler:
MsgBox "Error #: " & Err.Number & " " & Err.Description
Resume Exit_This_Sub
End Sub

my for next loop is not working and gives error94

Cannot understand why my for and next loop is not working. What I am trying to do is to print a report for the same number of records details depending on the number value of a particular field called [nts].
If the field is empty it tells me I have error: 94. If the field has a value it goes to errorhandler mention in the program. Can any body be so kind and help me please? Thank you in advance :)** I am using ACCESS2007
Option Compare Database
Option Explicit
Private Sub Report_Close()
' Delete previous data from tabMeal
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete from tabAmeal"
End Sub
Private Sub Report_Load()
Dim intNOM As Integer
'NOM means number of nights meals
Dim mTimes As Integer
On Error GoTo errorhandler
intNOM = 1
mTimes = 0
mTimes = DLookup("nz([nts],0)", "tabAmeal", "[nts] > 0")
'mTimes means number of meals
If mTimes = 0 Then
MsgBox "File is empty GO to Query" & vbCrLf & "Error - Run the Query", vbQuestion
Else
'now print the information found in table "tabmeal" number of times depending on the value field "nts"
For intNOM = 1 To mTimes
DoCmd.OpenReport "repAmeal?", acViewPreview
Next intNOM
End If
errorhandler:
MsgBox "Error #:- " & Err.Number & vbCrLf & "LOOP not working" & vbCrLf & "Must find why this error" & vbCrLf & Err.Description
End Sub
Private Sub Report_NoData(Cancel As Integer)
MsgBox "Please note that you have no records to report." & vbCrLf & "You have to run the QUERY to get the required informatio."
End Sub
The first problem:
Nz() must be used outside of DLookup, not inside:
mTimes = Nz(DLookup("[nts]", "tabAmeal", "[nts] > 0"), 0)
The second problem, I'm not sure. You can't open a report multiple times in acViewPreview (it will open only once), but it shouldn't give an error.
What is the exact error message you get when nts > 0 ?
Oops. You need Exit Sub befor your errorhandler: line - the code simply enters the error handler, without there being an error. :)

How can I remove the pesky "The runCommand action was canceled" dialog box after a "cancel = true" statement

On the Form_beforeupdate() event of my form "Alias" I have this...
If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If
Anytime this cancels a runCommand code such as...
DoCmd.RunCommand acCmdSaveRecord
a dialog appears telling me that it was canceled. Is there a way to suppress those dialogs?
You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord
The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.
Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().
Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!
Private Sub cmdSave_Click()
'DoCmd.RunCommand acCmdSaveRecord
SaveRecord
End Sub
Private Sub SaveRecord()
Dim strMsg As String
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 ' The RunCommand action was canceled.
' do nothing --> just ignore the error
Case Else
' notify user about any other error
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure SaveRecord"
MsgBox strMsg
End Select
Resume ExitHere
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtBlock_start.Value) Then
MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
Cancel = True
End If
End Sub
I ended up doing this on a 'Save and Open the Receipts form' button.
Private Sub btnSaveOpenReceipts_Click()
'''''SaveRecord'''''
If (Me.Dirty) Then
SaveRecord
End If
'''''OpenReciepts'''''
If (Me.Dirty) Then 'unsure it saved
Exit Sub
Else
'blah blah blah
End If
End Sub

disabling the bypass key

I would like to disable the bypass key of my database on the open form event during the autoexec so that the user is not able to view the underlying tables of my form. I have found the following code and creatd a module to run upon opening the form during the auto exec. The module is called SetBypass
Call SetBypass
Option Compare Database
Public Function SetByPass(rbFlag As Boolean, File_name As String) As Integer
DoCmd.Hourglass True
On Error GoTo SetByPass_Error
Dim db As Database
Set db = DBEngine(0).OpenDatabase(File_name)
db.Properties!AllowBypassKey = rbFlag
setByPass_Exit:
MsgBox "Changed the bypass key to " & rbFlag & " for database " & File_name, vbInformation, "Skyline Shared"
db.Close
Set db = Nothing
DoCmd.Hourglass False
Exit Function
SetByPass_Error:
DoCmd.Hourglass False
If Err = 3270 Then
' allowbypasskey property does not exist
db.Pro perties.Append db.CreateProperty("AllowBypassKey", dbBoolean, rbFlag)
Resume Next
Else
' some other error message
MsgBox "Unexpected error: " & Error$ & " (" & Err & ")"
Resume setByPass_Exit
End If
End Function
The above module need to be called form out side the application
try the below code if you are in same database
Sub blockBypass()
Dim db As Database, pty As DAO.Property
Set db = CurrentDb
On Error GoTo Constants_Err 'Set error handler
db.Properties("Allowbypasskey") = False
db.Close
Constants_X:
Exit Sub
Constants_Err:
If Err = 3270 Then 'Bypass property doesn't exist
'Add the bypass property to the database
Set pty = db.CreateProperty("AllowBypassKey", dbBoolean _
, APP_BYPASS)
db.Properties.Append pty
Resume Next
End If
MsgBox Err & " : " & Error, vbOKOnly + vbExclamation _
, "Error loading database settings"
End Sub

problems with DoCmd.OpenReport

I have a button on a form that opens a report. The report open as it should whenever there is data however when there is no data in the report I get an error and the debugger takes me to this line
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
Here is the entire code:
`With Me
If ((IsNull(.StartDate) Or .EndDate = "")) Then
Beep
MsgBox "You must enter report start period", vbOKOnly, "Required Data!"
ElseIf (IsNull(.EndDate) Or .EndDate = "") Then
Beep
MsgBox "You must enter report end date", vbOKOnly, "Reqired Data"
ElseIf (.StartDate > .EndDate) Then
Beep
MsgBox "The report start date must be earlier than the end date", vbOKOnly, "Required Data"
Else
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
End If
End With`
I need to complete this by tomorrow so any help will be much appreciated. Thanks
Access reports have a NoData event. You can put the actions you want to run in the case of no data there. If you set Cancel to True inside the NoData event, the report will not open or print. If you leave Cancel as its default False, the report will open as it does now.
That said, you also need to catch this error when using DoCmd.OpenReport:
On Error Goto ReportFail
DoCmd.OpenReport strReport, acViewPreview
ExitHere:
Exit Sub
ReportFail:
If Err=2501 Then
'OpenReport was canceled
Err.Clear
Resume ExitHere
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
I'm not really extremely experienced with vba in access but why don't you try puttting
DoCmd.OpenReport "FilesToBeReturnedReport", acViewReport, "", "", acNormal
after the
End with