Why does my Access database keep crashing? - vba

I was happily creating an Access Database in MS Access 2007 and I recently created a simple logon and splash screen form. Now, everytime i try to enter a string into my textbox, the program keeps crashing.
Error message says: Microsoft Access has stopped working. Windows can try and recover your data.
Remedial Actions Taken:
I tried the 'Compact & Repair Database' option. (no good)
I tried to delete the logon and splash screens (crashes)
I trield Alt+F11 to access the VB code (crashes again)
Below is the simple VB code i inserted at the start:
Option Compare Database
Public intlogonattempts As Integer
Private Sub cmdOK_Click()
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this
'matches value chosen in combo box
If Me.txtPassword.Value = "password" Then
DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmSplash_Screen"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intlogonattempts = intlogonattempts + 1
If intlogonattempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
Is there anything underlying here that is affecting/corrupting the program?
thanks

Related

Required Fields Before Update in Conjunction with Close Button

Novie VBA programmer here, looking for the ABC's direction.
I have 3 fields that I need to require in a form before the form is close.
I had previously require the fields within the Table; However, when I create a new contact to add to this Form I need to Requery the results of the added contact. When I Requery and the required fields are Required from the Table I reach a stuck, since I need to add the new contact but cannot Requery until the fields are filled (and I cannot do that until I have the right contact).
I know this is in the wrong place but this is the code I have thus far attached to my "Close" button.
Private Sub SaveCloseBtn_Click()
If Nz([PlaintiffName], "") = "" Then
MsgBox "Plaintiff Name is required.", vbExclamation, "Required Field"
Cancel = True
End If
If Nz([LawFirm], "") = "" Then
MsgBox "Law Firm is required.", vbExclamation, "Required Field"
Cancel = True
Me.[LawFirm].SetFocus
End If
If [PlaintiffName] = True Then
If [LawFirm] = True Then
If MsgBox("You are about to exit this case. Are you sure?", vbYesNo, "Warning!") = vbYes Then
DoCmd.Close
Else
DoCmd.CancelEvent
End If
End Sub
I tried creating Required fields in "BeforeUpdate" but for whatever reason, it does not work in conjunction with the docmd.close form function.

Checking if a TextBox password field does not have any value or empty in Access

How will I know if a password textbox field in access is Empty or there's no any value on it?
Private Sub cmdLogin_Click()
If IsNull(Me.txtPassword.Value) Or Trim(Me.txtUserName & "") = vbNullString Then
MsgBox "Username or Password must not be empty.", vbCritical, "Error!"
Else
MsgBox "welcome"
End If
End Sub
The code is what I've tried and even though I tried using Len(Me.txtPassword & "") = 0, it still executes the if statement. My concern is that if the textbox fields' input mask is a password, it should accept the space(by pressing spacebar) as a text or part of the string. Is there any way I can figure this out?
You are specifically not allowing a blank space as a password in your original if statement. This should work:
Private Sub cmdLogin_Click()
If IsNull(Me.txtPassword.Value) Then
MsgBox "Username or Password must not be empty.", vbCritical, "Error!"
Else
MsgBox "welcome"
End If
End Sub
I think it is irrelevant though. Surely what matters is that the password matches the one stored in the database? Personally, I would set the password field in the table to Required = True and AllowZeroLength = False.
In MS Access it is very hard to update a field value to something ending " ". In datasheet view on the table and on forms, when a text field is updated, the value is automatically trimmed. It's quite annoying.

I'd like to make "Any Part of Field" Default (Access VBA)

In a simple Find action dialog box on an MSAccess (2007) form I want to make "Any Part of Field" the default value when the Find and Replace box appears.
The actual default value is "whole field". I though that I could change that with the following line:
DoCmd.FindRecord " ", acAnywhere, , , , , False
But that doesn't make any difference. The rest of the code works fine (associated with a command button). But that above line does nothing whether it's there or not. Please help. I have the following code:
Private Sub AppNAppFind_Click()
On Error GoTo AppNAppFind_Click_Err
On Error Resume Next
Err.Clear
DoCmd.FindRecord " ", acAnywhere, , , , , False
DoCmd.RunCommand acCmdFind
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
AppNAppFind_Click_Exit:
Exit Sub
AppNAppFind_Click_Err:
MsgBox Error$
Resume AppNAppFind_Click_Exit
I'm using Access 2007.
The following code works for me in Access 2010.
Private Sub myFind_Click()
DoCmd.GoToControl "=[Screen].[PreviousControl].[Name]"
DoCmd.FindRecord " ", acAnywhere, False, acSearchAll, False, acCurrent, False
DoCmd.RunCommand acCmdFind
End Sub
I verified that the Access default for "Find" is "Whole Field" on this machine, yet when I click my button the Find dialog has "Any Part of Field" selected for "Match".
I'm not sure if this is an option in Access 2007, but in Access 2016, you can go to File > Options > Client Settings and look for Default find/replace behaviour and change it from Fast search to General search and it will set the default find settings to Look in: Current document and match in Any Part of Field.
This setting is for the client and will be effective in any access db you open and is persistent.

VB compile error when trying to use String.Compare

I am trying to check if the password is the same or not (in terms of caps too) using vb.net. But the line
result = String.Compare(actPassword,userPassword)
keep giving error "Compile Error: Expected:("
Private Sub Login_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cmbLoginID) Or Me.cmbLoginID = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cmbLoginID.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPW) Or Me.txtPW = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPW.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this matches value chosen in combo box
Dim userPassword As String
Dim actPassword As String
Dim result As Integer
userPassword = txtPW.Text
actPassword = DLookup("EmpPassword", "Employee", "EmployeeID='" + Me.cmbLoginID.Value + "'")
result = String.Compare(actPassword,userPassword)
If result = -1 Then
'Close logon form and open splash screen
DoCmd.Close acForm, "Login", acSaveNo
DoCmd.OpenForm "HomePage"
Else
MsgBox "Password Invalid. Please Try Again", vbCritical + vbOKOnly, "Invalid Entry!"
Me.txtPW.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database. Please contact your system administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
That's because there is no String.Compare() in VBA (There is in VB.NET)
Also, please note VB.NET is not VBA
Use StrComp
Use StrComp
result = StrComp(actPassword, userPassword, vbTextCompare)
While String.Compare() exists in VB.Net, I don't think it does in VBA. In any case, what you're doing is wrong anyway.
String.Compare can return any integer and it will only return zero if they match. Your particular test (comparing against -1) is only checking one possibility, that the actual password is less than the desired one, and it's only checking for one of the possible negative values.
You would be better off in that case with something like:
if result <> 0 Then
However, if you just want to compare two strings for equality in VBA (rather than figure out the relative ordering), you can just use:
if actPassword = userPassword Then

Trouble trapping 2501 error

I am sending data from frmSearchEmployeeWorksheets to frmStatsCorr which runs a query (qryStatsCorr). On frmStatsCorr I am checking to make sure the query returns records otherwise I will Msg the user and return to the search form. My problem is that I am having problems 'ignoring' the 2501 caused by the DoCmd.OpenForm ("frmStatsCorr") which I learned here on Stackoverflow...
What am I doing wrong that is causing me major Access VBA Frustration??
This is the sub on the Search form (frmSearchEmployeeWorksheets):
Private Sub btnSearch_Click()
' I only change focus to force the updated data to submit to query
Me.[txtEmployee].SetFocus
Me.txtShift.SetFocus
If txtUnit = "7" Then
'First close the form in order to update
DoCmd.Close acForm, "frmStatsCorr"
' Open Stats form
On Error GoTo myErr
**DoCmd.OpenForm ("frmStatsCorr") 'causes error**
End If
myExit:
Exit Sub
myErr:
Echo True
If Err.Number = 2501 Then GoTo myExit
MsgBox Err.Description
GoTo myExit
End Sub
In frmStatsCorr I simply check to make sure the query returns records if not I inform the user, close the form, and return to the frmSearchEmployeeWorksheets
Private Sub Form_Load()
If strFormStatus = "view" Then
If DCount("*", "qryStatsCorr") = 0 Then
MsgBox "Your search does not produce any results. Try a different search.", vbOKOnly
DoCmd.Close
DoCmd.OpenForm ("frmSearchEmployeeWorksheets")
Exit Sub
End If
txtDay = WeekdayName(Weekday(Me.WorkDate)) 'This line returns an error so I check for an empty query and return to the search form.
Me.[WorkDate].SetFocus
Me.txtUnit.Enabled = False...
I'm unsure how well I understand your code or the logic behind it. My hunch is you should check the DCount result from btnSearch_Click, and not fiddle with closing then re-opening frmStatsCorr, and having frmStatsCorr close itself when it contains no data. Just do not open frmStatsCorr when it will not contain data.
If the current form (frmSearchEmployeeWorksheets) which holds your btnSearch_Click procedure contains unsaved data changes, you can save them with Me.Dirty = False
Private Sub btnSearch_Click()
Dim strPrompt As String
If Me.Dirty Then ' unsaved data changes
Me.Dirty = False ' save them
End If
If Me.txtUnit = "7" Then
If DCount("*", "qryStatsCorr") = 0 Then
strPrompt = "Your search does not produce any results. " & _
"Try a different search."
MsgBox strPrompt, vbOKOnly
Else
' if frmStatsCorr is open, just Requery
' else open frmStatsCorr
If CurrentProject.AllForms("frmStatsCorr").IsLoaded Then
Forms("frmStatsCorr").Requery
Else
DoCmd.OpenForm "frmStatsCorr"
End If
' uncomment next line to close current form
'DoCmd.Close acForm, Me.Name
End If
End If
End Sub
If frmStatsCorr is open and you need to check whether it is in Design View, examine its CurrentView property.
Forms("frmStatsCorr").CurrentView ' Design View = 0
I suggested that approach because I suspected frmStatsCorr's Form_Load may trigger the 2501 error when it closes itself. But I'm not certain that's the cause of the error and I'm not motivated enough to set up a test.
If you still have 2501 errors with the approach I suggested, there are two other possible causes I've encountered:
corruption
broken references