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
Related
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 am developing in ASP VBScript at work and need to run a SELECT DISTINCT query but I am having some troubles.
I have other queries in my code that work perfectly fine, that do not use SELECT DISTINCT.
Here is what I am using:
Dim sections()
c = 1
set conn=Server.CreateObject("ADODB.Connection")
set rs=Server.CreateObject("ADODB.Recordset")
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=******;User ID=Admin;" & _
"DATA SOURCE=" & Server.MapPath("modules.mdb")
rs.open "SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC",conn
ReDim sections(10)
do while not rs.EOF
sections(c) = rs("section")
c = c + 1
rs.MoveNext
loop
rs.Close
conn.Close
set rs = nothing
set conn = nothing
Which gives me this error:
error '80004005'
on the line of the SQL query
The only way to fix this is to use "GROUP BY" instead of "DISTINCT"
SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC
SELECT section FROM modules WHERE area='First' GROUP BY section ORDER BY lvl ASC
The query retrieves a single record as is confirmed by the recordcount but every single row in the table gets updated
I am using vb6 and ms ado 2.8
The Firebird version is 2.5.4.26856 (x64).
Firebird ODBC driver 2.0.3.154
The computer is windows 7 home edition 64 bit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
If I alter the query slightly by also selecting a second column, the client surname then only rows with the same value in the surname column as that of of the row where the clientid is 10021 get edited.
sQuery = "select memo, surname from clients where clientID = 10021 "
I cannot understand how more than one row should be edited when the recordset contains only a single row
EDIT: Having read around the web a bit this is my understanding of what is happening.
It seems that the update method identifies which records to update based on the selected columns in the recordset.
So if you select fields a,b,c,d and are updating field a, it will only update records in the database whose values for a,b,c,d match those in the recordset.
The best way to ensure that you only update a single record is to include the primary key in the selected fields.
So if I had written my query as in the line below, only a single record would have been updated because the clientID column contains unique values.
sQuery = "select memo, clientID from clients where clientID = 10021 "
It makes sense thinking about it but the way I wrote the query originally seems to work fine, in my experience, with other databases or am I wrong?
I tested your code everything was fine and only update one row. I only want to suggest you a simple way to check whether record exist or not. that could be like this:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if
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 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.