Microsoft Access, vbYesNo can't get record to save - vba

Question on Access, I've created a vbYesNo on the save record button which asks me if is a new record, if it is a yes it saves as a new record, otherwise I need it to Update the existing record.
Private Sub btnSaveDetails_Click()
Dim Response As Integer
Response = MsgBox("Do you wish to create a new record?", vbYesNo, "Continue?")
If IsNull(txtLocation) Then
MsgBox "Please Enter Location Details"
Else
If Response = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToRecord , , acNewRec
Me.Refresh
Me.cboSelectLocation = ""
txtLocation.SetFocus
Else
DoCmd.RunCommand acCmdUndo
End If
End If
End Sub
The Problem basically is that on the Save button it asks if it is a new record, if it is a vbNo I need it to update the current record.

Different method to yours, but this is how I do mine:
If CloseResponse = vbNo Then
Exit Sub
Else
CurrentDb.Execute "UPDATE tbl_Suggestions_Historic SET [Status] = 'Closed By User', [ReasonClosed] = '" & ClosedWhy & "' WHERE ID = '" & ID & "'"

Related

MSAccess Form Search-Button conflicts with Edit-Button

I'm new to this forum and quite new to Access. I have the following problem. I've created an Form/Subform to edit the Data of a Query. Two Controls seem to be in conflict in my code.
"Search_Order" is an unbound text field. If text is entered and enter is pressed the corresponding fields of a query are shown. The code looks like the following:
Set rs_Search = Me.RecordsetClone
rs_Search.FindFirst "[OrderNumber]=""" & Search_Order & """"
If rs_Search.NoMatch Then
MsgBox "Sorry, no such record '" & Search_Order & "' was found.", _
vbOKOnly + vbInformation
Else
Me.Recordset.Bookmark = rs_Search.Bookmark
End If
rs_Search.Close
Search_Order = Null
Set rs_Search = Nothing
End Sub
The second command "ButtonSetOrderDetails10" should create a RecordsetClone of the Subform "sfrmChangeOrderDetails" and change the Value of the Field "OrderStatus" to the Vlaue of "10".
It has this code:
Private Sub ButtonSetOrderDetails10_Click()
Dim rs_Status_Change As DAO.Recordset
Set rs_Status_Change = Me.sfrmChangeOrderDetails.Form.RecordsetClone
With rs_Status_Change
Do While Not .EOF
.Edit
.Fields("OrderStatus") = 10
.Update
.MoveNext
Loop
End With
rs_Status_Change.Close
Set rs_Status_Change = Nothing
End Sub
I've looked both codes here up and modified them to the needs of my database. Both codes work fine so far, but unfortunately only once.
My problem is that as soon as I hit the Button "ButtonSetOrderDetails10" I can't do the same trick with a different order. I can search for the other order, it is displayed but the Button "ButtonSetOrderDetails10" does not work anymore. If I close the Form and reopen it, it works again.
It would be great if someone can give me a hint what I'm doing wrong here.
Best regards, Ferdi
I am surprised even works one time. With DAO recordset need to read dataset into memory before it will be able see records for edit otherwise just sees EOF and the loop doesn't run. Try:
rs_Status_Change.MoveLast
rs_Status_Change.MoveFirst
With rs_Status_Change
Don't even need to declare/set/close a recordset object.
Private Sub Search_Order_AfterUpdate()
With Me.RecordsetClone
.FindFirst "[OrderNumber]='" & Me.Search_Order & "'"
If .NoMatch Then
MsgBox "Sorry, no such record '" & Me.Search_Order & "' was found.", _
vbOKOnly + vbInformation
Else
Me.Bookmark = .Bookmark
End If
End With
Me.Search_Order = Null
End Sub
Private Sub ButtonSetOrderDetails10_Click()
With Me.sfrmChangeOrderDetails.Form.RecordsetClone
.MoveLast
.MoveFirst
Do While Not .EOF
.Edit
.Fields("OrderStatus") = 10
.Update
.MoveNext
Loop
End With
End Sub
Could really simplify code by running an UPDATE action SQL.
Private Sub ButtonSetOrderDetails10_Click()
CurrentDb.Execute "UPDATE ChangeOrderDetails SET OrderStatus=10 WHERE OrderNumber='" & Me.OrderNumber & "'"
End Sub

Runtime Error 15 - Type Mismatch for code to change a password in MS Access database

I get a Runtime Error 15 on the following line:
MyuserID = Me.txtfirstname.Value from the code below:
Option Compare Database
Option Explicit
Private Sub cmdchange_Click()
On Error Resume Next
If Trim(Me.txtnewpass & "") <> Trim(Me.txtconfirmpass & "") Then
MsgBox "Passwords do not match", vbExclamation + vbOKOnly, ""
Me.cmdchange.Enabled = False
Else
Me.cmdchange.Enabled = True
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Select' From[User Registration Details] where [UserID]=" & MyuserID)
If Not rs.EOF And Not rs.BOF Then
rs.Edit
rs("Password") = txtconfirmpass
rs.Update
rs.Close
Set rs = Nothing
MsgBox "Your Password has been successfully changed", vbInformation, "Electporl"
DoCmd.Close acForm, "frmnewpassword", acSaveNo
DoCmd.OpenForm "frmlogin"
End If
End If
Given that I placed the code below on the button that takes the user to the changing password form.
Private Sub cmdproceed_Click()
If IsNull(Me.txtfirstname) Or Me.txtfirstname = "" Then
Me.mand1.Visible = True
Me.txtfirstname.SetFocus
End If
If IsNull(Me.txtemail) Or Me.txtemail = "" Then
Me.mand2.Visible = True
Me.txtemail.SetFocus
End If
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("User Registration Details", dbOpenSnapshot, dbReadOnly)
rs.FindFirst ("Firstname='" & Nz(Me.txtfirstname, "") & "'")
If rs.NoMatch = True Then
Me.lbl1.Visible = True
Me.txtfirstname.SetFocus
Exit Sub
End If
If rs!Username <> Nz(Me.txtemail, "") Then
Me.lbl2.Visible = True
Me.txtemail.SetFocus
Exit Sub
End If
'MyuserID is publicly declared as Long in a module
MyuserID = Me.txtfirstname.Value
DoCmd.OpenForm " frmnewpassword"
DoCmd.Close acForm, Me.Name
End Sub
the second code is assigned to the button that redirects the user to the form that will enable him or her change the password after verifying his or her first name and email.
The second one now is assigned to the button that will help the user change the password by overwriting the old password.
Please pass the UserID value in your procedure.
In your cmdproceed_Click() procedure update the following section:
'MyuserID is publicly declared as Long in a module
MyuserID = rs("UserID")
In your cmdchange_Click() procedure update the following line:
Set rs = CurrentDb.OpenRecordset("Select * From [User Registration Details] where [UserID]=" & MyuserID)
From a logical point of view, you can have others with the same firstname, so doing the filter on the firstname only will introduce unexpected behaviors later in the life cycle of your application.
If you have two or more users with the first name 'Joshua' then your code will always select the first user with that first name. You need to update this logic to select a unique user.

MS Access: How does my Insert Into Statement has to look like if i want to insert only the record i changed/modified into a new table?

I have a continues form where the user can edit data, stored in the table, directly.
Before the user can close the form or edit a different record he is asked if he wants to save his changes.
I use the Before Update Event for that:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Msg Box Nachricht bestimmen.
strMsg = "M?chten Sie die ?nderungen speichern?" & Chr(10)
strMsg = strMsg & "Dr?cken Sie Ja um zu speichern oder Nein um die ?nderungen zu verwerfen."
'Msg Box anzeigen.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Speichern?")
'Nutzer Antwort pruefen
If iResponse = vbNo Then
'Aenderungen verwerfen.
Me.Undo
'Update abbrechen.
Cancel = True
End If
End Sub
When the form loads i get the current value of the text field by using this:
Private Sub Form_Load()
TempVars("Status") = txtStatus.Value
End Sub
Now if the User changes this text field the record (with the old value for txtStatus) should be saved in a new table.
This code WITHOUT the "Where"-Part copies all the records in my table, into the new table:
Dim Status As String
Dim Counter As Integer
Counter = 0
If txtStatus.Value <> TempVars("getStatus") Then
Status = txtStatus.Value
Counter = Counter + 1
End If
Dim sql As String
Dim dbs As DAO.Database
Set dbs = CurrentDb()
sql = "Insert into tblStatusFahrzeugeHistory (F_ID, Status, seit, bis, von, an, Bemerkung, Datenbank_Nutzer, Eintrag_erstellt_am, Eintrag_erstellt_um)" & _
"Select tblStatusFahrzeuge.F_ID, tblStatusFahrzeuge.Status, tblStatusFahrzeuge.seit, tblStatusFahrzeuge.bis, tblStatusFahrzeuge.von, tblStatusFahrzeuge.an, tblStatusFahrzeuge.Bemerkung, tblStatusFahrzeuge.Datenbank_Nutzer, tblStatusFahrzeuge.Eintrag_erstellt_am, tblStatusFahrzeuge.Eintrag_erstellt_um " & _
"From tblStatusFahrzeuge Where txtStatus.Value = Status "
dbs.Execute sql, dbFailOnError
dbs.Close
Set dbs = Nothing
End If
WITH the "Where"-Part i attempted to insert only the record i changed on the form into the new table but the code does'nt work. I get this error code: runtime error'3061' 1 parameters were expected, but too few parameters were passed.Does someone know what im doing wrong?
The query engine cannot "see" txtStatus.Value. You will either have to add parameters to your query, or concatenate in the value of the text box into the query text:-
sql = "Insert into tblStatusFahrzeugeHistory (F_ID, Status, seit, bis, von, an, Bemerkung, Datenbank_Nutzer, Eintrag_erstellt_am, Eintrag_erstellt_um)" & _
"Select tblStatusFahrzeuge.F_ID, tblStatusFahrzeuge.Status, tblStatusFahrzeuge.seit, tblStatusFahrzeuge.bis, tblStatusFahrzeuge.von, tblStatusFahrzeuge.an, tblStatusFahrzeuge.Bemerkung, tblStatusFahrzeuge.Datenbank_Nutzer, tblStatusFahrzeuge.Eintrag_erstellt_am, tblStatusFahrzeuge.Eintrag_erstellt_um " & _
"From tblStatusFahrzeuge Where '" & txtStatus.Value & "' = Status "

My Login Method for Access 2007 doesn't work

This is my first try at VBA on the whole. So I'm not familiar with everything.
I'm trying to make a login for a database in Access 2007.
I have 2 Tables named TableLogin and TableUserLevel.
TableLogin has ID, Username, Password and UserLevel that is linked to TableUserLevel.
TableUserLevel has ID and UserLevel which has the levels of Admin and User.
I have a form named LoginForm that has 2 fields named txtUsername and txtPassword and a button named cmdLogin.
I'm pretty sure my DLookUp methods are wrong, but I don't know why. I'm using the first DLookUp to see if the username and password match one ID and if so it will then go to the second DLookUp method to see which userlevel security that particular user has.
Right now, I'm not sure the first DLookUp even works good. I'm not sure if it checks if both input came from one ID. I'm hoping it works like that and the last DLookUp method only return 0 and I don't know why.
The VBA code for the button is:
Private Sub cmdLogin_Click()
Dim UserLevel As Integer
Dim Username As String
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
MsgBox "You must enter a Username.", vbOKOnly, "Required Data"
Me.txtUsername.SetFocus
Exit Sub
End If
'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 username and password in tblLogin to see if this
If IsNull(DLookup("ID", "TableLogin", "Username = '" & Me.txtUsername.Value & "'") And DLookup("ID", "TableLogin", "Password = '" & Me.txtPassword.Value & "'")) Then
MsgBox "Password or Username invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Else
Username = Me.txtUsername.Value
If IsNull(DLookup("UserLevel", "TableLogin", "Username = '" & Me.txtUsername.Value & "'")) Then
MsgBox ("No security level")
Else
If UserLevel = 1 Then
MsgBox ("yes" & UserLevel)
Else
MsgBox ("No" & UserLevel)
End If
End If
'Close login form and open screen
DoCmd.Close acForm, "LoginForm", acSaveNo
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

ms access query to create a new login id, each time i wish to add a new user through a user details form

I have a Login table containing user information like, login id, username, password and access level. So i created a form called user details form where i can add new users into the database.
each time i open the user details form, a new id is generated by running a query.
this is what i typed in the field of the query to generate a new id,
new_login_ID: "LI" & Right([Login_ID],2)+1
the problem is, in my login form, i try to login with the following code:
Private Sub LoginBtn_Click()
'Check to see if data is entered into the UserName combo box
Dim lngMyEmpID As Long
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
lngMyEmpID = Me.cboEmployee.Value
'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 tblAdmins to see if this matches value chosen in combo box
If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]=" & lngMyEmpID) Then
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
Me.txtPassword = Null
intLogonAttempts = intLogonAttempts + 1
'If User Enters incorrect password 3 times database will shutdown
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
Else
Me.txtPassword = Null
'Open correct form
Dim strAccessLevel As String
strAccessLevel = DLookup("[Access]", "tbl_login", "[Login_ID]=" & lngMyEmpID)
If strAccessLevel = "Admin" Then
MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
DoCmd.Close
DoCmd.OpenForm "frm_Admin"
ElseIf strAccessLevel = "Manager" Then
'MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID")
MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
DoCmd.Close
DoCmd.OpenForm "frm_main_menu"
End If
End If
End Sub
I get a runtime error saying "mismatch" and when i debug, it points to this line of my login code - lngMyEmpID = Me.cboEmployee.Value
I dont know how to fix this, can someone please explain where i've gone wrong and how i can fix this problem.. thanks in advance :)
This line you have written in the comments should be corrected to this (since lngMyEmpID is now a string) :
If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]='" & lngMyEmpID & "'")
Same goes for every line you use DLookup and the ID in this manner.