VB compile error when trying to use String.Compare - vb.net

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

Related

VBA code works in one Access Database, but not another. All of the supporting objects have been imported. What I am not seeing?

This code is for a login form I have setup to enter the front end copy of a database. In one database it works like a charm. I imported the form and corresponding objects to another database and it appears to be not recognizing my password when I enter it. I just keep getting the message box I have popping up that says, "Incorrect password". Any help figuring this out would be greatly apprecaited.
The SQL statement that drives the cboUser combo box on the login form is:
SELECT tblUser.UserID, [FName] & " " & [LName] AS Fullname,
tblUser.Password, tblUser.PWReset, tblUser.AccessLevelID
FROM tblUser ORDER BY tblUser.LName, tblUser.FName;
.
Private Sub OkBTN_Click()
Static intIncorrectCount As Integer
Dim AuthorityNumber As Integer
'Dim rs As Recordset
'TempVars("Username") = Me.cboUser.Value
'Column references for cbouser row source reference
'UserID = 0
'FullName = 1
'Password = 2
'PWReset = 3
'AccessLevelID = 4
'Set rs = CurrentDb.OpenRecordset("UserNameQuery", dbOpenSnapshot)
'N = Nz(DLookup("Fullname", "UserNameQuery", "Fullname=""" & Me.cboUser & """"), " ")
'Check that User is selected
If IsNull(Me.cboUser) Then
MsgBox "You forgot To Select your name from the drop down menu!", 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) Then
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
End If
Me.Visible = FALSE
intIncorrectCount = 0
'Main menu after correct login based on AuthorityNumber
If Me.cboUser.Column(4) = 5 Then
DoCmd.OpenForm "SRL1MainMenu"
'Forms!AMSReportForm!L2Menubtn.Visible = False
Forms!SRL1MainMenu!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
Else
DoCmd.OpenForm "L2MainMenu2"
'Forms!AMSReportForm!L2Menubtn.Visible = True
Forms!L2MainMenu2!FullNameLoggedIn = Forms!frmLogin!cboUser.Column(1)
End If
'Failed login attempt limitation
ElseIf intIncorrectCount > 1 Then
MsgBox "Too many failed login attempts. Click OK To Set New password", vbOK + vbExclamation
DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
'DoCmd.Close acForm, "frmLogin"
Else
MsgBox "Incorrect password", vbOKOnly + vbExclamation
Me.txtPassword = Null
Me.txtPassword.SetFocus
intIncorrectCount = intIncorrectCount + 1
End If
End If
End Sub
First, #Andre is probably right - you may have to apply brackets to "Password".
Next, this may not perform a case sensitive comparison:
If Me.txtPassword = Me.cboUser.Column(2) Then
Use StrComp to do that:
If StrComp(Me!txtPassword.Value, Me!cboUser.Column(2), vbBinaryCompare) = 0 Then
Finally, you should never store plain passwords; store a hash value instead. It isn't that difficult, if you study my latest article:
Storing passwords in VBA using the Microsoft NG Cryptography (CNG) API

MS Access: Why is my code not being reached?

Im trying to do some error handling for my code and I want my custom error message to appear if the user is trying to enter an already existing record. Access gives its own standard error message indicating a duplicate record, but I want mine displayed instead. The issue is the part of the code where I have my custom error message isn't being reached, therefore giving me the default message.
The name of the textbox is "DepartmentCode", the name of the table its being drawn from is "tDepartment" and the column name is "DepartmentCode"
My code is this...
Private Sub bAddDepartment_Click()
On Error GoTo bAddDepartment_Click_Err
Dim OKToSave As Boolean
OKToSave = True
If Not SomethingIn(Me.DepartmentCode) Then ' Null
Beep
MsgBox "A department code is required", vbOKOnly, "Missing Information"
OKToSave = False
Else
Dim myDepartmentCode As String
myDepartmentCode = "DepartmentCode = " + Chr(34) + Me.DepartmentCode + Chr(34)
If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) <> Null Then
MsgBox "Department already on file", vbOKOnly, "Department already on file."
OKToSave = False
End If
End If
If OKToSave Then
' If we get this far, all data is valid and it's time to save
Me.Dirty = False
DoCmd.GoToRecord , "", acNewRec
End If
bAddDepartment_Click_Exit:
Exit Sub
bAddDepartment_Click_Err:
Resume bAddDepartment_Click_Exit
End Sub
The part not being reached is If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) <> Null Then
Why is this happening?
Debugging VBA Code <-- to see which lines are actually executed.
If DLookup("DepartmentCode", "tDepartment", myDepartmentCode) <> Null Then
You can't compare to Null like that. Try this in the Immediate Window:
? ("foo" <> Null)
Null
Use IsNull()
If Not IsNull(DLookup("DepartmentCode", "tDepartment", myDepartmentCode)) Then
or if empty strings are also possible, use Nz()
If Nz(DLookup("DepartmentCode", "tDepartment", myDepartmentCode), "") <> "" Then

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.

VBA in Access. How to require a field if another field has been answered

I am working on an Access DB and need to require that ScrapAmount1 has an answer if they have put data into ScrapCodes1. I want to have an error message pop up that reminds them to make the amount of scrap higher than 0. I'm using VBA and here is what I have that is not working. PS. ScrapCodes1 is a drop down list that they choose from and the ScrapAmount is 0 by default.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapCodes1 Then
If Nz(Me.ScrapAmount1, "") = "" Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value.", vbExclamation, "Data Required"
End If
End If
End Sub
Thank you for your answers in advance. :)
I don't think you need anything more than:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.ScrapAmount1= 0 Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value.", vbExclamation, "Data Required"
End If
End Sub
Your if is using the Nz to check if ScrapAmount1 is null. ScrapAmount1 defaults to 0, so it's not null.
I'd have a second If condition myself:
'check for null
If Nz(Me.ScrapAmount1, "") = "" Then
Cancel = True
MsgBox "If Scrap Code is selected, then Scrap Amount must have a value.", vbExclamation, "Data Required"
'checks for 0 and negative numbers
ElseIf Me.ScrapAmount1 <= 0 then
Cancel = True
MsgBox "Scrap Amount must be higher than 0"
End If
If Cancel then
'do cancel stuff, I have no idea what you use this var for
end if

Why does my Access database keep crashing?

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