Struggling to solve MS Access OpenForm was cancelled - error 2501 - ms-access

'Check to see if the recordset actually contains rows
If Not (recordSet.EOF And recordSet.BOF) Then
recordSet.MoveFirst 'Unnecessary in this case, but still a good habit
'See if credentials match data
Do
If (recordSet!User_ID = PERSAL And recordSet!Password = Password) Then
'Open Menu form
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
'Close Login form
DoCmd.Close acForm, "Login"
Exit Do
End If
recordSet.MoveNext
If (recordSet.EOF Or recordSet.BOF) Then
MsgBox "Your credentials are incorrect or you are not registered."
Exit Do
End If
Loop
'Match the values entered for PERSAL nr. and password fields with a row in User table
Else
MsgBox "There are no records in the recordset."
recordSet.Close 'Close the recordset
Set recordSet = Nothing 'Clean up
End If
Form_Login.txtUser_ID.SetFocus
I have tried every solution I've found for the past 2 days but nothing has worked to solved my error 2501:
I did:
Decompiling
Compact and repair
Tackling the MSCOMCTL.OCX file
Importing to a new access file
I am spent.
For the MSCOMCTL.OCX file I'm thinking to download a 2007 Office update SP 3.
The exception/error appears on this line:
DoCmd.OpenForm "Menu", acNormal, "", "", , acNormal

Your syntax is not correct. Try:
DoCmd.OpenForm "Menu", acNormal, , , , acWindowNormal

Related

Access 2010 email VBA

I was wondering if anyone can help me.
I'm currently making an access 2010 DB which has the function to send tickets to other people.
I used the template provided by Access, which has the function to email the ticket to whom is it assigned to. Which is great. BUT I can't figure out how to get more than just the TITLE field in the body of the email.
I would like to have a body of text and a few more fields from the ticket if possible? Can anyone help me out?
I've converted the OnClick macro to VBA which I've pasted below.
How do I change this to do what I want?
Option Compare Database
'------------------------------------------------------------
' Macro1
'
'------------------------------------------------------------
Function Macro1()
On Error GoTo Macro1_Err
With CodeContextObject
On Error Resume Next
DoCmd.SendObject , "", "", DLookup("[E-mail Address]", "Contacts", "[ID]=" & Nz(.[Assigned To], 0)), "", "", "Duplicate for your attention", IIf(.Form.Description.TextFormat = 1, PlainText(.Title), .Title), True, ""
If (.MacroError.Number <> 0) Then
Beep
MsgBox .MacroError.Description, vbOKOnly, ""
End If
End With
Macro1_Exit:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function
I ones found this on the internet and used it for my ticketsystem: the varbody value contains the messagetext
Private Sub Command430_Click()
On Error GoTo ErrorHandler
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = DLookup("[E-mail Address]"
'separate each email by a ','
varSubject = "Project " & [Forms]![Yourform]![Projectnr] & " contains a new ticket"
'Email subject
varBody = "Please pick up the following ticketnumer: " & [Forms]![Yourform]![Ticketnr]
'Body of the email
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
'Send email command. The True after "varBody" allows user to edit email before sending.
'The False at the end will not send it as a Template File
ErrorHandler:
Select Case Err.Number
Case 2501
MsgBox ("No email send")
End Select
End Sub

VBA Code Error 2450 After changing to ACCDE

I have a function that has been in use for a number of months that checks to see if the form that is going to be opened will actually have records to be viewed before opening it. Recently I decided to change from ACCDB to ACCDE for security purposes. After making the change over the function started throwing error 2450 "Microsoft Access cannot find the referenced form..." I can't seem to find anything of use online that could tell me what the cause of this error is and why it only happens with ACCDE.
On a side note I realize the inefficiency of the logic in this function, it's on my list.
Public Function ValidateFormToOpen(strFormName As String, strFilter As String, strFieldName As String) As Boolean
On Error GoTo Err_Handler
Dim intNumberOfRecords As Integer
'If the form is currently open count how many results will be shown
If CheckFormState(strFormName) Then
intNumberOfRecords = DCount(strFieldName, Access.Forms(strFormName).RecordSource, strFilter)
'If it is closed open it in a hidden state and then count how many records would be shown
Else
DoCmd.OpenForm strFormName, acDesign, "", strFilter, , acHidden
intNumberOfRecords = DCount(strFieldName, Access.Forms(strFormName).RecordSource, strFilter)
DoCmd.Close acForm, strFormName
End If
'If there were records that will be shown return true
If intNumberOfRecords > 0 Then
ValidateFormToOpen = True
Else
ValidateFormToOpen = False
End If
Exit_Handler:
Exit Function
Err_Handler:
Call LogError(Err.Number, Err.Description, strMODULE_NAME & ".ValidateFormToOpen on " & strFormName)
Resume Exit_Handler
End Function
This is the CheckFormState Code
Public Function CheckFormState(sFormName As String) As Boolean
On Error GoTo Err_Handler
If Access.Forms(sFormName).Visible = True Then
CheckFormState = True
End If
Exit_Handler:
Exit Function
Err_Handler:
CheckFormState = False
Resume Exit_Handler
End Function
An ACCDE format database restricts design capabilities in general. I think that may be why you get an error with this line:
DoCmd.OpenForm strFormName, acDesign, "", strFilter, , acHidden
However I'm not positive that is the complete explanation because when I attempt to open a form in Design View (DoCmd.OpenForm "Form1", acDesign) in my ACCDE database, Access gives me a different error message:
"The command you specified is not available in an .mde, .accde, or .ade database."
So I don't know what the solution is for your goal, but I believe it can not be based on opening a form in Design View.

DoCmd.OpenForm crashing Access 2010

I have a user login form on my access database with a table of users and other user criteria. One is a checkbox that if "true" will open up a change password form when the following code is ran. But when this checkbox field returns true, access 2010 crashes. So far, Google has left me empty handed. Any ideas on what would be causing the crash? Is it something in the code or could it be an issue with the "change password" form? The form opens just fine when I manually open it by "double clicking" it.
'Check if password needs to be reset
If Me.cboUser.Column(3) = True Then
DoCmd.OpenForm "frmPasswordChange", , , "UserID = " & Me.cboUser
End If
The entire code is this:
Private Sub txtPassword_AfterUpdate()
'Check that EE is selected
If IsNull(Me.cboUser) Then
MsgBox "You need to select a user!", vbCritical
Me.cboUser.SetFocus
Else
'Check for correct password
If Me.txtPassword = Me.cboUser.Column(2) Then
'Check if password needs to be reset
If Me.cboUser.Column(3) = True Then
DoCmd.OpenForm "frmPasswordChange", , , "UserID = " & Me.cboUser
End If
DoCmd.OpenForm "Opening Screen"
Me.Visible = False
DoCmd.SetWarnings False
DoCmd.RunSQL ("INSERT INTO tblAuditTrail ([DateTime], UserName, Action) VALUES (Now(),'" & Me.cboUser.Column(1) & "','Login')")
DoCmd.SetWarnings True
Else
MsgBox "Password does not match, please re-enter!", vboOkOnly
Me.txtPassword = Null
Me.txtPassword.SetFocus
End If
End If
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

Optimize VBA code that exports to PDF from MS Access

I have two functions that will open and save two diffrent reports based on the same criteria. They are identical except for the refrences:
Function Export_MLR()
On Error GoTo Export_MLR_Err
Dim strReportName As String
DoCmd.OpenReport "Market Rate Notification Final", acViewPreview
strReportName = "S:\National Installations\Market Labor Rates\MLR_INV\MLR\" & Format (Reports![Market Rate Notification Final].Market_ID, "00") & " " & Replace(Reports![Market Rate Notification Final].Product_Code, " / ", "_") & "-" & "Market Rate Notification Final" & "_" & Format(Date, "mmddyy") & ".pdf"
DoCmd.OutputTo acOutputReport, "Market Rate Notification Final", "PDFFormat(*.pdf)", strReportName, False, , , acExportQualityScreen
DoCmd.Close acReport, "Market Rate Notification Final", acSaveNo
Export_MLR_Exit:
Exit Function
Export_MLR_Err:
MsgBox Error$
Resume Export_MLR_Exit
End Function
Then I created this function to select the data and put it into the table that the reports refrence line by line:
Function MassMarket()
On Error GoTo MassMarket_ERR
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
'this query creates my rs1 recordset'
DoCmd.SetWarnings (warningsOff)
DoCmd.OpenQuery "mass_market", acNormal, acEdit
DoCmd.SetWarnings (warningsOn)
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Mass_market_Rate_change")
Set rs2 = db.OpenRecordset("tbl_Form_Auto")
'this checks and clears any records in rs2'
If rs2.EOF = False And rs2.BOF = False Then
rs2.MoveFirst
rs2.Delete
End If
rs1.MoveFirst
'loop goes through and adds 1 line runs reports saves them and deletes line'
Do Until rs1.EOF
Set rs2 = db.OpenRecordset("tbl_Form_Auto")
rs2.AddNew
rs2![MarketID] = rs1![MarketID]
rs2![Product_ID] = rs1![Product_ID]
rs2.Update
Call Export_Invoice
Call Export_MLR
rs1.MoveNext
rs2.MoveFirst
rs2.Delete
Loop
MassMarket_Exit:
Exit Function
MassMarket_ERR:
MsgBox Error$
Resume MassMarket_Exit
End Function
Now all of this worked like a charm but it created, on average 16 .pdf files per minute and I had to create 820 .pdf files(about 50 minutes). If this is the best I can do then I will take it but would love to cut this time in half if possible. Thanks for any and all input. NR
In a comment you indicated the bulk of the time is spent in your functions which export the reports to PDF. I'm uncertain whether we can speed those up.
It seems you open a report, reference a value in that report for use as part of the PDF file name, then call the OutputTo method with the same report name to save it as PDF.
In that situation, I'm uncertain what happens "under the hood" ... whether Access opens a second instance of the report object, or is smart enough to see that you already have an instance open and just use that one instead.
As a test, try to signal Access to use the first report instance. From the online help for the ObjectName parameter of the OutputTo method:
If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank.
So what I'm suggesting is try this in your code:
DoCmd.OutputTo acOutputReport, , "PDFFormat(*.pdf)", _
strReportName, False, , , acExportQualityScreen
If Access complains, try it with an empty string for the ObjectName parameter.
DoCmd.OutputTo acOutputReport, "", "PDFFormat(*.pdf)", _
strReportName, False, , , acExportQualityScreen
I don't know how much (or even if) this suggestion will speed up your code. But if you can't speed up those export functions somehow, my hunch is your hope to cut the overall time by half is a shaky proposition.