“Hey guys, I’m stuck on this problem and I could really use some help finding where I went wrong. I’ve checked my work a dozen times, but I just can’t seem to spot the mistake. Any ideas or suggestions would be super appreciated. Let’s solve this puzzle together!”
This is the code:
Private Sub cmdSave_Click()
Dim Customer_ID As String
Dim Customer_Name As String
Dim Address As String
Dim Contact_info As String
Customer_ID = Me.txtCustomer_ID.Text
Customer_Name = Me.txtCustomer_Name.Text
Address = Me.txtAddress.Text
Contact_info = Me.txtContact_info.Text
If Customer_ID = "" Or Customer_Name = "" Or Address = "" Or Contact_info = "" Then
MsgBox "Please enter all the needed info."
Exit Sub
End If
' Check if the connection is established
If ConnectToDatabase Is Nothing Then
MsgBox "Connection to the database is not established."
Exit Sub
End If
' Execute SQL query to check if the course already exists
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM customer WHERE Customer_ID='" & txtCustomer_ID.Text & "'", ConnectToDatabase
' Check if the query returned any records
If Not rs.EOF Then
MsgBox "customer already exists. Please choose a different Customer ID."
rs.Close
Exit Sub
End If
' Close the recordset
rs.Close
' Execute SQL query to insert the new course record
Dim insertSQL As String
insertSQL = "INSERT INTO customer (Customer_ID, Customer_Name,Address,Contact_info) VALUES ('" & Customer_ID & "', '" & Customer_Name & "','" & Address & "','" & Contact_info & "')"
ConnectToDatabase.Execute insertSQL
MsgBox "Successfully saved!"
' Clear text boxes after successful registration
Me.txtCustomer_ID.Text = ""
Me.txtCustomer_Name.Text = ""
Me.txtAddress.Text = ""
Me.txtContact_info.Text = ""
frameCourse.Enabled = False
frameSave.Enabled = False
frmMain.Enabled = True
End Sub
New contributor
Russell Quero is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.