INSERT INTO adds leading and trailing spaces - ms-access

Why does INSERT INTO add leading and trailing spaces to strings in the following:
CurrentDb.Execute "INSERT INTO [Rounds] ([Round ID],[Cource ID],[Round Date],[Tee Time],[Handicap Round],[Player ID],[Player HCP],[Slope],[Tee],[Holes Played],[Comments],[Green Fee],[GF Term])" & _
" VALUES (' " & TempVars!tvNewRoundID & " ',' " & TempVars!tvCourceID & " ',' " & TempVars!tvRoundDate & " ',' " & TempVars!tvTeeTime & " ',' " & TempVars!tvHandicapRound & " ',' " & TempVars!tvPlayerID & " ',' " & [Forms]![Data Entry Form]![txtPlayerHCP] & " ',' " & TempVars!tvPlayerSlope & " ',' " & TempVars!tvTeeName & " ',' " & TempVars!tvHolesPlayed & " ',' " & TempVars!tvComments & " ',' " & TempVars!tvGreenFee & " ',' " & TempVars!tvGreenFeeTerm & " ' );"
Declarations are like this:
Public tvNewRoundID As TempVars
Public tvRoundDate As TempVars
Public tvTeeName As Variant etc.
The problem applies for strings only, everything else works fine. I have tried by trimming before INSERT INTO with no avail, the spaces appear in the table.

Related

Ms Access SQL VBA query error no 3075

In my Access application, I am trying to concat existing field value with new value and I have code as below written in VBA, I am getting error 3075 "syntax error missing operator"
sUser = UserNameWindows
' MsgBox sUser
currenttime = Format(Now(), "dd/mm/yyyy hh:mm:ss")
SqlQuery = "UPDATE tbl_AllRequests " & _
" SET [Status]= 'Archived' " & _
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
" WHERE [ID] in (" & selectedIDs & ")"
'" SET [History] = concat([History], 'Archived on " & currenttime & " by '" & sUser & "'&')'&" & _
'data=concat(data, 'a')
'SqlQuery = "UPDATE tbl_AllRequests SET Status = 'Archived' WHERE ID in (17, 11)"
Debug.Print "this is new one " & SqlQuery
DoCmd.RunSQL SqlQuery, True
I am getting error on below line of code.
", [History] = [History] & CHR(13) & CHR(10) & 'Archived on' " & currenttime & " ' by ' & '" + sUser + "' " & _
If I remove the code after 'Archived on' it works.
Thanks
First correct this:
currenttime = Format(Now(), "dd/mm/yyyy hh:nn:ss")
Then, use only single quotes around your fixed text:
", [History] = ([History] + CHR(13) + CHR(10)) & 'Archived on '" & currenttime & "' by '" & sUser & "" & _
Also, it makes no sense first to format Now to a string expression, then convert (with errors) to a date value by wrapping it in octothorpes (#..#), which - when concatenated with the strings - will be casted once more to a string.

Edit Update not working on MS Access?

I'm new to MS Access. This is a basic stuff. I'm doing an edit button in MS Access forms, but I keep getting runtime errors. Here it is the code:
CurrentDb.Execute " UPDATE FLIGHT SET " & _
"STD= ' " & Me.Text3 & " ' " & _
",Destination = ' " & Me.Text5 & " ' " & _
",Remark = ' " & Me.Text7 & " ' " & _
",ETD = ' " & Me.Text9 & " ' " & _
",ATD = ' " & Me.Text11 & " ' " & _
",OFBL = ' " & Me.Text13 & " ' " & _
",CAR= ' " & Me.Text15 & " ' " & _
",Nature = ' " & Me.Text17 & " ' " & _
",REG_N0 = ' " & Me.Text19 & " ' " & _
"WHERE FLIGHT_NR = " & Me.Text0
flightsubform.Form.Requery
Is the Field FLIGHT_NR a integer field (1,145) or a text field (KQ145)?
If its a integer field you might need to change your Where statement to capture Me.Text0 as an integer as below:
" WHERE FLIGHT_NR = " & int(Me.Text0)
If its a text field you might need to add quotes around your Me.Text0 value to capture it as text:
" WHERE FLIGHT_NR = " & " ' " & Me.Text0 & " ' "
Edit:
Change your code for debugging as follows:
My_SqlText = " UPDATE FLIGHT SET " & _
"STD= ' " & Me.Text3 & " ' " & _
",Destination = ' " & Me.Text5 & " ' " & _
",Remark = ' " & Me.Text7 & " ' " & _
",ETD = ' " & Me.Text9 & " ' " & _
",ATD = ' " & Me.Text11 & " ' " & _
",OFBL = ' " & Me.Text13 & " ' " & _
",CAR= ' " & Me.Text15 & " ' " & _
",Nature = ' " & Me.Text17 & " ' " & _
",REG_N0 = ' " & Me.Text19 & " ' " & _
" WHERE FLIGHT_NR = " & " ' " & Me.Text0 & " ' "
Debug.print My_SqlText 'Will print it to immediate window for inspection
CurrentDb.Execute My_SqlText
UPDATE FLIGHT SET STD= ' 4:55:00 PM ' ,Destination = ' ATH ' ,Remark = ' DEP ' ,ETD = ' ' ,ATD = ' 5:15:00 PM ' ,OFBL = ' 5:05:00 PM ' ,CAR= ' A3 ' ,Nature = ' J ' ,REG_N0 = ' ZA_A309 ' WHERE FLIGHT_NR = ' A3 847 '
UPDATE FLIGHT SET STD= ' 4:55:00 PM ' ,Destination = ' ATH ' ,Remark = ' DEP ' ,ETD = ' ' ,ATD = ' 5:15:00 PM ' ,OFBL = ' 5:05:00 PM ' ,CAR= ' A3 ' ,Nature = ' J ' ,REG_N0 = ' ZA_A309 ' WHERE FLIGHT_NR = ' A3 847 '

number of query values and destination fields are not the same

This is what I have for the Add Event.
CurrentDb.Execute "INSERT INTO tblMatchBook ([Patron], [Staff], [TimeReceived], [SurveyLink], [DateFinished], [BookTitles]) " & _
" Values('" & Me.ddBarcode & "," & Me.ddStaff & "," & Me.txtRDate & "," & Me.txtLink & "," & "" & "," & "" & "')"
I cannot figure out what is wrong.
insert into Main values (28494,1,False,'Buto-asma Sirop' , 'Buto-asma Sirop', 3.99 , 'Syrup', 'ispani', ' ', ' ',0, '1',4988 )

MYSQL "Column count doesn't match value count at row 1"

I'm getting the title error when attempting to execute the following:
sInsertInto = "INSERT INTO 5why (date, op_id, serial, why1, why2, why3, why4, why5, root_cause, lessons) VALUES (" + _
"'" + f_date + "', " + _
"'eccross', " + _
" '" + f_partnum + "', " + _
" '" + f_first + "', " + _
" '" + f_second + "', " + _
" '" + f_third + "', " + _
" '" + f_fourth + "', " + _
" '" + f_fifth + "', " + _
" '" + f_root + "'" + _
" '" + f_lessons + "'" + _
")"
The value count should be fine here; but its not for whatever reason and I cannot get this error to resolve.
You seem to be missing a comma, specifically after the f_root line. I would think this would be a syntax error due to the two strings 'f_root' 'f_lessons', but I guess not.
Your query is vulnerable to injection. You should be using parameterized queries with prepared statements. Depending upon the variable values, this may also be the problem.

how to concatenate sql query in newline in vb6.0?

i want to concatenate sql query that is not in single line but new line .
Conn.Execute "insert into users("
"FirstName,"
"LastName,"
"Address1,"
"Address2,"
")values("
" UCase(txtfirstname.Text) &" ,
"& UCase(txtlastname.Text) &",
" & UCase(txtaddress1.Text) & ",
" & UCase(txtaddress2.Text) & "
")"
how to concatenate them into single one?
Thanks ,
Yugal
In the VB6 to do a line continuation you end a line with [space][underscore]. You also need to use the & to concatenate the text segments on each line together. So the line would look something like this:
Conn.Execute "insert into users(" _
& "FirstName," _
& "LastName," _
& "Address1," _
& "Address2" _
& ")values(" _
& UCase(txtfirstname.Text) & "," _
& UCase(txtlastname.Text) & "," _
& UCase(txtaddress1.Text) & "," _
& UCase(txtaddress2.Text) _
& ")"
Something like this perhaps:
Conn.Execute "insert into users(FirstName, LastName, Address1, Address2) values('" & UCase(txtfirstname.Text) & "', '" & UCase(txtlastname.Text) & "', '" & UCase(txtaddress1.Text) & "', '" & UCase(txtaddress2.Text) & "'")"