I am unable to use a variable as the vale for a table, though direct values are stored.
I get the error as "Syntax error in INSERT INTO Statement.How do I overcome this ?
sDBPAth = App.Path & "\SETMDBPATH.mdb"
sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDBPAth & ";" & _
"Jet OLEDB:Engine Type=4;"
' Type=4 to create an Access97 mdb, If omitted Access2000 mdb
' ------------------------
' Create New ADOX Object
' ------------------------
Set oDB = New ADOX.Catalog
oDB.Create sConStr
Set oCn = New ADODB.Connection
oCn.ConnectionString = sConStr
oCn.Open
Set oCM = New ADODB.Command
oCM.ActiveConnection = oCn
oCM.CommandText = "CREATE TABLE MDBPATH(" & _
"MDBPATH TEXT(40) NOT NULL," & _
"pass TEXT(10))"
oCM.Execute
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES (sDBPAth, 1,)" 'If 'sDBPath' is used the word sDBPath is stored not the variable value
'
' ------------------------
' Release / Destroy Objects
' ------------------------
If Not oCM Is Nothing Then Set oCM = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing
If Not oDB Is Nothing Then Set oDB = Nothing
End Sub
Try
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You had an extra comma at the end, and you need to pass the variable in outside of the string.
You're relatively save in this case since you're not pass in a user input, but if you continue to build INSERT statements like above, you'll be vulnerable to SQL Injection attacks.
You need to amend your SQL statement to allow the application to substitute the sDBPath value
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You need to call the variable outside of the string.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
If sDBPAth is an actual VB variable, you need to use something like:
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', '1')"
so that the insert statement is constructed from the value of the variable rather than a fixed string.
In addition, you appear to have a superfluous comma in your statement and you may need quotes around both values since they're both text type.
Related
Below script capture in a raw text file. But I want to save input in a csv file into cell. Kindly help
Dim employee_id, employee_name, system_running_status
employee_id = InputBox("Enter your Employee ID", "Employee ID")
employee_name = InputBox("Enter your Name", "Employee Name")
fission_running_status = InputBox("Did you Powered-On the system today (Yes/No) ?","Fission Server Status")
Set obj = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 8
Set obj1 = obj.OpenTextFile("aap.txt", ForWriting)
obj1.WriteLine ("Employee: " & employee_name & "-" & employee_id & " turned on system on: ") & Now() & vbCr
obj1.Close
Set obj=Nothing
You can simply reformat your output string:
Set obj1 = obj.OpenTextFile("aap.csv", ForWriting)
obj1.WriteLine employee_name & "," & employee_id & "," & Now() & vbCr
I'm using a VBA code in excel to pick some data and insert it into an Access DB. I need this for several tables.
The problem is: For some of those tables, I get an error like: "INSERT INTO syntax error". But, if I get the string the code generates and uses for inserting, and use it in SQL mode form Access, the query works just fine. So that doesn't make any sense. Here is a piece of it:
For j = 6 To lastrow
SQLStr = "INSERT INTO TENSILE(REFERENCE, REF_ID, POSITION, RATIO, YSL0, YSL90, YSL180, YSL270, YST0, YST90, YST180, YST270, UTSL0, UTSL90, UTSL180, UTSL270, UTST0, UTST90, UTST180, UTST270, EL0, EL90, EL180, EL270, ET0, ET90, ET180, ET270, ARL0, ARL90, ARL180, ARL270, ART0, ART90, ART180, ART270) SELECT '" & ws3.Cells(j, 1) & "', REF.ID,'" & ws3.Cells(j, 60).Value & "'"
For i = 61 To 93
SQLStr = SQLStr & "," & ws3.Cells(j, i).Value
Next i
SQLStr = SQLStr & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
ws3.Cells(7, 3).Value = SQLStr
MsgBox (SQLStr)
'rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
it's important to notice that this same structure is used for other tables and works normaly, like in:
For j = 6 To lastrow
SQLStr = "INSERT INTO METALOGRAPHY(REFERENCE, REF_ID, AUSTGRAINSIZE) SELECT '" & ws3.Cells(j, 1) & "',REF.ID ," & ws3.Cells(j, 18).Value & " FROM REF WHERE REF.REFERENCE LIKE '" & ws3.Cells(j, 1) & "'"
'MsgBox (SQLStr)
'ws3.Cells(2, 3).Value = SQLStr
rs.Open SQLStr, con, adOpenStatic, adLockOptimistic 'Opening the query
Next j
What is going wrong?
Have you considered changing how you are linked to the Access DB so that you could use a statement like:
db.execute SQLStr
This should solve your problem
I have a requirement to read one complete column values from Access database and need to sort it in a random order/ Descending order and update the sorted values to another column so , that one name is assigned to another name. That to only using VB script.
I know only how to read the values from Access database i don't know further steps..
Could any one help me in this?
One Recordset sort Desc, second Recordset sort Asc. Then loop through the first RS and update the table with the first RS value where table value equals second RS value.
In this example change ColumnName, ColumnName2 and TableName according to your database. ColumnName2 is output column so it will be replaced with new values.
Sub AddDescToAsc()
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS2 As DAO.Recordset
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName DESC", dbOpenForwardOnly)
Set MyRS2 = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName ASC")
MyRS2.MoveFirst
With MyRS
Do While Not .EOF
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE TableName SET ColumnName2 = '" & .Fields("ColumnName") & "' WHERE ColumnName = '" & MyRS2(0) & "'"
DoCmd.SetWarnings True
MyRS2.MoveNext
.MoveNext
Loop
End With
MyRS.Close
MyRS2.Close
End Sub
I am noticing that the number of records in a database table (select reference from datetable) suddenly increases when I start running the program below even though there are no new records added. Please note that I establish that the number of rows increases by running a query in SQL Studio Manager i.e. select reference from datetable. When the program stops; the number of records falls back to the original level. Here is the code. Why does this happen? There is no Primary Key in the table though Reference is unique.
rs.Open "select reference,value1,datefield from datetable where field1 = 'value1' " & _
"order by reference", objAuditCon.ActiveCon, adOpenStatic, adLockPessimistic
Do While Not rs.EOF
intReadCount = intReadCount + 1
DoEvents
If Not IsNull(rs("value1")) Then
testArray = Split(rs("value1"), ",")
rs2.Open "SELECT Date FROM TBL_TestTable WHERE Record_URN = '" & testArray(1) & "'", objSystemCon.ActiveCon, adOpenStatic, adLockReadOnly
If rs2.EOF Then
End If
If Not rs2.EOF Then
rs("DateField") = Format$(rs2("Date"), "dd mmm yy h:mm:ss")
rs.Update
intWriteCount = intWriteCount + 1
End If
rs2.Close
Else
End If
rs.MoveNext
Loop
rs.Close
It's a little confused, but if you are referring to the total given by "intReadCount" against how many rows you have in the table then it looks as though you are not correctly clearing this value. At the beginning of the procedure you would want to set "intReadCount" back to 0 before starting, you should then get constant results.
Updated: See comments below
When I select the value from the combo box, related value should appear in the text box
ComboBox code.
cmd.CommandText = "select distinct PERSONID from T_PERSON"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("PersonID")) Then
txtno.AddItem rs("PersonID")
End If
rs.MoveNext
Wend
In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid
Text Box
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("Name")) Then
txtName.Text = rs("Name")
rs.MoveNext
End If
Wend
I put the above code in Form_Load Event, Nothing displaying in Text Box.
What wrong in my code.
Need VB6 code Help
You would want the 2nd block of code in the the click event for the combobox.
Edit
There looks like another couple of issues in your code at this line:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
2 Issues:
You are passing in the control itself as the person ID, not the selected value.
You have an extra space in your query after the person ID
You should change that line to be:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
Why not have the combobox display the name and hold the personID as it's item data?
cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
Set rs = cmd.Execute
While Not rs.EOF
combo.AddItem rs("Name").value
combo.ItemData(combo.NewIndex) = rs("PERSONID").value
rs.MoveNext
Wend
Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).