Column count doesn't match value count at row 1 - mysql

im taking too much time for this error and can't find the error here
Adapter = New MySqlDataAdapter("Insert into tbluser(UserID,Name,UserType,Password) values('" & txtUserId.Text & "' ,'" & txtName.Text & "','" & cmbUserType.Text & "','" & "',AES_ENCRYPT('" & txtpass.Text & "','x'))", connection)

Adapter = New MySqlDataAdapter("Insert into tbluser (UserID
,Name
,UserType
,Password)
values( '" & txtUserId.Text & "'
, '" & txtName.Text & "'
, '" & cmbUserType.Text & "'
, '" & "'
, AES_ENCRYPT('" & txtpass.Text & "','x'))", connection)
You have more values than you have defined columns to insert. It looks like the problem is an empty field between the UserType and the Password. Revise it like so should resolve the column count error:
Adapter = New MySqlDataAdapter("Insert into tbluser ( UserID
, Name
, UserType
, Password)
values( '" & txtUserId.Text & "'
, '" & txtName.Text & "'
, '" & cmbUserType.Text & "'
, AES_ENCRYPT('" & txtpass.Text & "','x'))"
, connection)

Related

Open Report Use Three Combobox Filter

I have 3 combobox to filter data, then i want to open in command button to print preview..
Problems I have if i used 2 combobox it works but when i used three combobox the report is blank...there's any problems with the code?? here's the code i used :
Dim strWhereCondition As String
strWhereCondition = "[month] = '" & Me.cbmonth.Value _
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Debug.Print strWhereCondition
DoCmd.OpenReport "Month List", View:=acViewPreview, _
WhereCondition:=strWhereCondition
and the requery for my form
SELECT *
FROM MyTable
WHERE (month = Forms!nameForm!cbmonth
OR Forms!MeForm!cbwork IS NULL)
And (works = Forms!nameForm!cbwork
OR Forms!nameForm!cbwork IS NULL)
AND (works = Forms!nameForm!cbwork2
OR Forms!nameForm!cbwork2 IS NULL);
can anyone help?
Month is a number, so try:
strWhereCondition = "[month] = " & Me.cbmonth.Value _
& " and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Edit: Month is text.
However, [Work] is supposed to match two potentially different values:
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [Works] = '" & Me.cbwork2.Value & "'"
Should most likely be:
& "' and [Works] = '" & Me.cbwork.Value _
& "' and [SomeOtherField] = '" & Me.cbwork2.Value & "'"

MYSQL-Access-Macro Error Unknown column in field list

I am trying to run an Excel macro that imports data from a csv file into an Access database, MySQL front end. The macro code is not new-we've been using this for a couple of years, the table in the Access DB is not new, and the csv files with which the data is used to be brought into the Access table is also unchanged (meaning same rows, same data types).
I am stumped as the error appears to be something of an anomaly and I have tried several fixes to correct the error. I have attached a copy of the error as well as the code below. The issue with the column referenced below as well error message attached, "itemcode", this column is in the database.
Clearly, there is something that I am missing. Any insights are greatly appreciated. Thank you.
'Read in Current .csv file
'Data Services
NumRows = Application.CountA(Range("A:A"))
For iCount = 1 To NumRows
Dim InstData(11)
InstData(1) = Range("A" & iCount) 'Date
invoiceDate = Format(InstData(1), "yyyy/mm/dd")
InstData(2) = Range("B" & iCount) 'Invoice Number
InstData(3) = Range("C" & iCount) 'names
flname() = Split(InstData(3), ",")
lname = flname(0)
fname = flname(1)
InstData(4) = Range("D" & iCount) 'Address1
InstData(5) = Range("E" & iCount) 'Address2
InstData(6) = Range("F" & iCount) 'City
InstData(7) = Range("G" & iCount) 'State
InstData(8) = Format(Range("H" & iCount), "00000") 'postal code
InstData(9) = Range("I" & iCount) 'phone
InstData(10) = Range("J" & iCount) 'email
InstData(11) = Range("K" & iCount) 'amount
Set conn = New ADODB.Connection
Set rec = New ADODB.Recordset
conn.Open "Dsn=HERIpub"
rec.Open ("Insert into tbl_Invoice (invoiceNumber,invoiceDate,invoiceAmount,invoiceDescription,invoiceFAU,invoiceCostCenter,invoiceProject,itemcode,invoicecFName,invoicecLName, invoicecAddr1, invoicecAddr2, invoicecCity, invoicecState, invoicecZIP, invoicecEmail, invoicecPhone) Values ('" & InstData(2) & "','" & invoiceDate & "','" & InstData(11) & "','Data Services','T6','T6','DATASL','40070DATASL','" & fname & "'" & ",'" & lname & "','" & InstData(4) & "', '" & InstData(5) & "', '" & InstData(6) & "', '" & InstData(7) & "', '" & InstData(8) & "', '" & InstData(10) & "', '" & InstData(9) & "')"), conn
macro error
Look at the variable fname , there you have redundant " & " . I think it should be:
rec.Open ("Insert into tbl_Invoice (invoiceNumber,invoiceDate,invoiceAmount,invoiceDescription,invoiceFAU,invoiceCostCenter,invoiceProject,itemcode,invoicecFName,invoicecLName, invoicecAddr1, invoicecAddr2, invoicecCity, invoicecState, invoicecZIP, invoicecEmail, invoicecPhone) Values ('" & InstData(2) & "','" & invoiceDate & "','" & InstData(11) & "','Data Services','T6','T6','DATASL','40070DATASL','" & fname & "','" & lname & "','" & InstData(4) & "', '" & InstData(5) & "', '" & InstData(6) & "', '" & InstData(7) & "', '" & InstData(8) & "', '" & InstData(10) & "', '" & InstData(9) & "')"), conn
Tell me if I'm right, or mark a vote up!

Access VBA/Prevent duplicate values

How I can prevent duplicate values to not insert into the table. I have created a code to INSERT, UPDATE and DELETE and I want to display a MsgBox that there is a duplicate value and to cancel it. Thanks. Below you have the code:
Private Sub Command12_Click()
If Me.emID.Tag & "" = "" Then
If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
Me.emID.BorderColor = vbRed
Me.emFirst.BorderColor = vbRed
Me.emLast.BorderColor = vbRed
MsgBox "Please fill required fields", vbInformation, "Information"
Exit Sub
End If
CurrentDb.Execute "INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment)" & _
"VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "')"
MsgBox "Record Added", vbInformation, "information"
Else
CurrentDb.Execute "UPDATE tblEmployees " & _
"SET emiD =" & Me.emID & _
", first ='" & Me.emFirst & "'" & _
", last = '" & Me.emLast & "'" & _
", gender ='" & Me.emGender & "'" & _
", phone = '" & Me.emPhone & "'" & _
", mobphone ='" & Me.emMob & "'" & _
", city ='" & Me.emCity & "'" & _
", state ='" & Me.emState & "'" & _
", zip ='" & Me.emZip & "'" & _
", adress ='" & Me.emAdress & "'" & _
", email ='" & Me.emEmail & "'" & _
", comment ='" & Me.emComment & "'" & _
"WHERE emID =" & Me.emID.Tag
MsgBox "Updated!", vbInformation, "Information"
End If
Me.tblEmployees_subform.Form.Requery
End Sub
It sounds like you'd like to update an employee if one exists for the given ID otherwise you'd like to add a new employee. You can prevent adding duplicate employees by first trying to update an employee record with the given ID and if no records were updated only then do you add a new employee record.
Private Sub Command12_Click()
If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
Me.emID.BorderColor = vbRed
Me.emFirst.BorderColor = vbRed
Me.emLast.BorderColor = vbRed
MsgBox "Please fill required fields", vbInformation, "Information"
Exit Sub
End If
' You must set CurrentDb to a variable otherwise the RecordsAffected
' property used later will be incorrect.
Dim db As DAO.Database
Set db = CurrentDb
' First try to update an existing employee.
db.Execute _
"UPDATE tblEmployees " & _
"SET first ='" & Me.emFirst & "', " & _
"last = '" & Me.emLast & "', " & _
"gender ='" & Me.emGender & "', " & _
"phone = '" & Me.emPhone & "', " & _
"mobphone ='" & Me.emMob & "', " & _
"city ='" & Me.emCity & "', " & _
"state ='" & Me.emState & "', " & _
"zip ='" & Me.emZip & "', " & _
"adress ='" & Me.emAdress & "', " & _
"email ='" & Me.emEmail & "', " & _
"comment ='" & Me.emComment & "'" & _
"WHERE emID =" & Me.emID.Tag & ";"
' If no records were affected by update then add a new employee.
If db.RecordsAffected = 0 Then
db.Execute _
"INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment) " & _
"VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "');"
MsgBox "Record Added", vbInformation, "Information"
Else
MsgBox "Updated!", vbInformation, "Information"
End If
Me.tblEmployees_subform.Form.Requery
End Sub
Note: In the update query I removed the update to the emID field since that is what the query is based on (in the WHERE clause). If the emID field is changing you won't be able to use the new emID value to find an employee record with the old emID value.
If you never want any duplicates I would also suggest that you add constraints to your database table to prevent duplicates, as suggested by Daniel Cook. I would also suggest looking into using parameterized queries instead of building SQL strings in VBA.
You can change your SQL to use IF EXISTS condition and insert only if the records does not already exists.
Your SQL may look like:
IF NOT EXISTS
(
SELECT ......
)
BEGIN
INSERT INTO tblEmployees ......<insert since employee does not exists>
END

run time error 2465

I am getting error in MS access 2003, Run Time Error 2465: Microsoft can't find the field '|' referred to in your expression.
Count = DCount("*", "PREPAYMENT_PRICING_CHANGE", ([INSERT_DATE] - Int([INSERT_DATE])) = ([Now()] - Int([Now()])))
db.Execute "INSERT INTO tbl_audit ([FilePath],[FileName],[action],[trans_date],[button],[number_of_records])" _
& " values ( '" & strFName & "', '" & File & "' , ""'Import_of_NTS_rate_PCD_file'"" ,Now() ,""'Import NTS rates from file'"",'" & Count & "');"
I think that you forgot the " in the DCount condition:
Count = DCount("*", "PREPAYMENT_PRICING_CHANGE", "([INSERT_DATE] - Int([INSERT_DATE])) = ([Now()] - Int([Now()]))")
Also try to escape your variables:
db.Execute "INSERT INTO tbl_audit ([FilePath],[FileName],[action],[trans_date],[button],[number_of_records])" _
& " values ( '" & Replace(strFName,"'","''") & "', '" & Replace(File,"'","''") & "' , ""'Import_of_NTS_rate_PCD_file'"" ,Now() ,""'Import NTS rates from file'"",'" & Count & "');"

Insert Into Code throws an error

I am using this code to insert 4 textboxes of data into a table.
CurrentDb.Execute = "INSERT INTO tbl_machineheader(Line No, Description, Service Interval, Type) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"
However it doesn't seem to work and I can't figure out why.
That "=" character is wrong:
CurrentDb.Execute **=** "INSERT INTO tbl_machineheader(Line No, Description, Service Interval, Type) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"
Also you should use square brackets for your column names. The correct syntax is as follows:
CurrentDb.Execute "INSERT INTO tbl_machineheader([Line No], [Description], [Service Interval], [Type]) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"