I have an Access table Users_T which has three fields: Computername, LoginID, Password
Computername is the only Primary Key
Now I have three variables:
Computername = Environ("username")
LoginID = Forms("Login").TextBoxID.Value
Password = Forms("Login").TextboxPassword.Value
Those three variables are NOT NULL.
Question: now if I have those three variable values, I want to check if Computername is already in the table User_T, if Computername already exists (LoginID and/or Password could be NULL), then update Login and Password with new variable values, no matter if LoginID/Password is NULL or not. If table User_T does not have the Computername, then add those three variables as a new record.
How should I write the SQL statement?
Not familiar with SQL syntax. My code is more about the logic. How to write it correctly? Or if there is simply way to write it? Something with one line of SQL to including both UPDATE and INSERT?
Thanks.
Code:
Dim Computername As String
Dim LoginID As String
Dim Password As String
Computername = Environ("username")
LoginID = Forms("Login").TextBoxID.Value
Password = Forms("Login").TextBoxPassword.Value
SqlExistStr = "SELECT ComputerUsername FROM User_T WHERE ComputerUsername = @Computername"
SqlUpdateStr = "Update User_T SET LoginID = @LoginID, Password = @Password WHERE ComputerUsername = @Computername"
SqlInsertStr = "INSERT INTO Users_T (Computername, LoginID, Password) VALUES (@Computername, @LoginID, @Password)"
IF EXISTS(DoCmd.RunSQL SqlExistStr)
DoCmd.RunSQL SqlUpdateStr
Else
DoCmd.RunSQL SqlInsertStr
End If