2 Syntax Error cant see :( (1 Viewer)

DaRTHY

Registered User.
Local time
Today, 03:28
Joined
Mar 6, 2015
Messages
90
Hi everyone,

I wrote some VBA Codes but I become errors. :banghead:


Code:
Private Sub btnAdd_Click()
    If Me.txtMail.Tag & "" = "" Then
        
         CurrentDb.Execute "INSERT INTO tblEmployees([E-Mail], [Name], [Surname], [Title], [City], [Phone_Number]) VALUES ('" & Me.txtMail & "', '" & Me.txtName & "', '" & Me.txtSurname & "', '" & Me.txtTitle & "', '" & Me.txtCity & "', '" & Me.txtPhone & "')"
    Else
         CurrentDb.Execute "UPDATE tblEmployees SET [E-Mail] = '" & Me.txtMail & "', [Name] = '" & Me.txtName & "', [Surname] = '" & Me.txtSurname & "', [Title] =  '" & Me.txtTitle & "', [City] = '" & Me.txtCity & "', [Phone_Number] = '" & Me.txtPhone & "' WHERE [E-Mail] =  Me.txtMail.Tag"
        
    End If
    
    btnClear_Click
    
    SubEmployees.Form.Requery
    
    End Sub

Private Sub btnClear_Click()
    Me.txtMail = ""
    Me.txtName = ""
    Me.txtSurname = ""
    Me.txtTitle = ""
    Me.txtCity = ""
    Me.txtPhone = ""
    
    Me.txtMail.SetFocus
    Me.btnEdit.Enabled = True
    Me.BtnAdd.Caption = "Add"
    Me.txtMail.Tag = ""
End Sub

Private Sub btnDlt_Click()
    If Not (Me.SubEmployees.Form.Recordset.EOF And Me.SubEmployees.Form.Recordset.BOF) Then
        If MsgBox("Are you sure to delete?", vbYesNo) = vbYes Then
            CurrentDb.Execute "DELETE FROM tblEmployees WHERE E-Mail = " & Me.SubEmployees.Form.Recordset.Fields("E-Mail")
            Me.SubEmployees.Form.Requery
        End If
    End If
    
            
End Sub

Private Sub btnEdit_Click()
    ' Check whether ther exists data'
    If Not (Me.SubEmployees.Form.Recordset.EOF And Me.SubEmployees.Form.Recordset.BOF) Then
    'get data to text box contol'
        With Me.SubEmployees.Form.Recordset
        Me.txtMail = .Fields("E-Mail")
        Me.txtName = .Fields("Name")
        Me.txtSurname = .Fields("Surname")
        Me.txtTitle = .Fields("Title")
        Me.txtCity = .Fields("City")
        Me.txtPhone = .Fields("Phone_Number")
        
        Me.txtMail.Tag = .Fields("E-Mail")
        
        Me.BtnAdd.Caption = "Update"
        Me.btnEdit.Enabled = False
        
       End With
    End If
End Sub

When i click delete button:
Im getting here Runtime error '3075'
Syntax error(missing operator) in query expression 'E-Mail = abc@def.com'


When i click add/update button:
Run-time error '3061'
too few parameters. Expected 1.



Hope so, you can help me .

Thanks.


PS: I'm not cutting my code with & _. Im writing until end of the line.
 

Minty

AWF VIP
Local time
Today, 10:28
Joined
Jul 26, 2013
Messages
10,353
E-Mail is a poor choice for a field name it could be interpreted as "e minus mail"
An E-Mail is a string this so should be
Code:
 CurrentDb.Execute "DELETE FROM tblEmployees WHERE [E-Mail] = '" & Me.SubEmployees.Form.Recordset.Fields("E-Mail") & "'"

You'll find it easier to debug if you use a string and debug.print it - e.g.
Code:
sSql =  "DELETE FROM tblEmployees WHERE [E-Mail] = '" & Me.SubEmployees.Form.Recordset.Fields("E-Mail") & "'" 
Debug.Print sSql
Currentdb.Execute sSql

The same applies to your update SQL string
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:28
Joined
Feb 28, 2001
Messages
26,996
Syntax error(missing operator) in query expression 'E-Mail = abc@def.com'

Besides Minty's comment about brackets, you also need to enclose abc@def.com in quotes as it is a string. When you DON'T supply quotes, what will happen is this: The line parser would see = followed by an unquoted text item. By syntax rules, abc without quotes will look like a variable. The @ sign is not a valid operator that I recall and it is not valid as part of a symbol name (only $ and underscore are, for VBA) so it "breaks" the parse at that point. What you have therefore is abc followed by an unknown operator followed by def.com so things get badly confused. It doesn't do this, but if I wrote the parser, it would point to the thing following abc as a variable followed by something and not separated from it by a known operator.

Regarding the use of special characters in a variable name, you should avoid that if at all possible (which it usually is). If you don't, you will need bracketing at every use of that variable. "E-mail" could be shortened to "Email" and would lose no clarity, I think, but you could use it without bracketing most of the time.
 

DaRTHY

Registered User.
Local time
Today, 03:28
Joined
Mar 6, 2015
Messages
90
thank you for your advice. I will change the field name. Than i will try it.
 

Users who are viewing this thread

Top Bottom