I have a central database that is kept on a network that stores part numbers & descriptions for various components for machines. Often times individuals will need to use an offline copy during design and will add new entries to it. Is there a script to find the differences and update the master file that is on the network? I played around with union queries but I'm struggling to be able to update the original file and original table. My sql/microsoft access knowledge is limited.
For sake of clarity let's call the files as such:
Network Database: Network_DB.mdb
Offline Database: Offline_DB.mdb
Table: MISC_CAT
In my experience a generic solution is pretty difficult. I tend to store the field names I want in a table then run the relevant update:
Public Function UpdateData()
Dim db As Database
Dim qd As QueryDef
Dim rs As Recordset
Dim strSQL As String
On Error GoTo Err_UpdateData
Set db = CodeDb
strSQL = "SELECT fldName FROM MyTables WHERE tblName = 'MISC_CAT'"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
Do Until rs.EOF
strSQL = "UPDATE MISC_CAT_Network INNER JOIN MISC_CAT_Offline ON MISC_CAT_Network.KeyID = MISC_CAT_Offline.KeyID " & _
"SET MISC_CAT_Network.[" & rs!fldName & "] = [MISC_CAT_Offline].[" & rs!fldName & "] " & _
"WHERE (((Nz([MISC_CAT_Network].[" & rs!fldName & "],''))<>Nz([MISC_CAT_Offline].[" & rs!fldName & "],'')))"
db.Execute strSQL, dbFailOnError
rs.MoveNext
Loop
db.Execute strSQL, dbFailOnError
rs.Close
db.Close
Exit_UpdateData:
Set rs = Nothing
Set qd = Nothing
Set db = Nothing
Exit Function
Err_UpdateData:
Debug.Print "Error - " & Err.Number & " - " & Err.Description
Resume Exit_UpdateData
End Function
Related
I have an issue while trying to get every line from a table in my database. In VBA when requesting the table with 'SELECT * FROM companies;', the results duplicates the first row, and remove the last one. As a result, I have 3 records, which corresponds to the real number of records in my DB, but instead of having 1, 2 and 3, I have 1, 1 and 2.
Any idea?
You can see here Database records for table 'companies', when requesting 'SELECT * FROM companies': DB Records
You can see here the result of the same request in Excel/VBA using the following code:
sqlQuery = "SELECT ALL * FROM companies;"
rsDB.Open sqlQuery, conDB, adOpenDynamic
Do While Not rsDB.EOF
For Each col In rsDB.GetRows
Debug.Print col
Next
Loop
Results: VBA request
Would love to get any piece of advice on this issue!
The fun fact is that if I try to select only one column of the table, such as 'idCompany', then I have the result '1, 2, 3' with VBA, which is fine. The real issue only appears when using '*'.
Thanks a lot for your time,
--- EDIT
The connection string used to connect to the DB:
Set conDB = New ADODB.Connection
Set rsDB = New ADODB.recordSet
Set rsDBCol = New ADODB.recordSet
conDB.connectionString = "DRIVER={MariaDB ODBC 3.1 Driver};" _
& "SERVER=s-mypricing-1;" _
& "DATABASE=db_pricing;" _
& "PORT=3306" _
& "UID=user;" _
& "PWD=pwd;" _
& "OPTION=3"
conDB.Open
rsDB.CursorLocation = adUseServer
rsDBCol.CursorLocation = adUseServer
Difficult to test, but I suspect you need this instead:
rsDB.MoveFirst
Do While Not rsDB.EOF
For Each fld In rsDB.Fields
Debug.Print fld.Name & ": " & fld.Value
Next
rsDB.MoveNext
Loop
When you iterate an ADO recordset, the object itself represents a current row. So you refer to the Fields of the current row to get the columns. And the properties of each field to get descriptive information about that cell (name of column, value in cell).
Through the comments we learned that the issue is related to opening the recordset with adOpenDynamic mode. What follows is code that should represent a working case for MaraiaDB.
Set conDB = New ADODB.Connection
Set rsDB = New ADODB.recordSet
Set rsDBCol = New ADODB.recordSet
conDB.connectionString = "DRIVER={MariaDB ODBC 3.1 Driver};" _
& "SERVER=s-mypricing-1;" _
& "DATABASE=db_pricing;" _
& "PORT=3306" _
& "UID=user;" _
& "PWD=pwd;" _
& "OPTION=3"
conDB.Open
rsDB.CursorLocation = adUseServer
rsDBCol.CursorLocation = adUseServer
sqlQuery = "SELECT ALL * FROM companies;"
With rsDB.Open(sqlQuery, conDB)
If Not (.BOF And .EOF) Then
.MoveFirst
Do Until .EOF
For Each fld In .Fields
Debug.Print fld.Name & ": " & fld.Value
Next
.MoveNext
Loop
End If
.Close
End With
conDB.Close
I'm really stuck. My coworkers and I cannot figure out why this database won't connect to "CurrentDb". Here's my code:
Dim db As Database, rs As DAO.Recordset
Dim strSQL As String, strRowSource As String
strSQL = "SELECT * FROM tbl_Documents"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
MsgBox "No Documents available!"
Exit Sub
End If
rs.MoveFirst
Do Until rs.EOF = True
strRowSource = strRowSource & rs!tbl_Documents.DocID & "," & rs!tbl_Document_Types.DocType & "," & rs!tbl_Documents.DocTypeID & "," & rs!tbl_Documents.DateReceived & "," & rs!tbl_Documents.LinkToFile & "," & rs!tbl_Documents.Comments & ";"
rs.MoveNext
Loop
Typically the error I get is "Item not found in this collection" during the Do Until loop. I put a watch on my database and recordset and it seems like neither are being set properly. I'm getting "Operation is not support for this type of object." in the connection field of the database object. Essentially, the exact same code is used for many other Access Databases that we have. Not sure why this won't play nice.
Looks to me like there are quiet a few changes that needs to be done to your code. As #OverMind has suggested, always declare the variables as they are. Specially libraries to avoid ambiguity in your code. Next, your strSQL includes only one table, but your strRowSource has another table. So your strSQL should be changed. I am not sure what the strRowSource does, but sounds to me like it is going to be a RowSource of a ListBox or ComboBox in that case, it is a bit confusing. Anyway your code should be.
Dim db As DAO.Database, rs As DAO.Recordset
Dim strSQL As String, strRowSource As String
strSQL = "SELECT * FROM tbl_Documents INNER JOIN tbl_Document_Types ON tbl_Documents.DocID = tbl_Document_Types.DocTypeID;"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
MsgBox "No Documents available!"
Exit Sub
End If
Do While Not rs.EOF
strRowSource = strRowSource & rs!DocID & "," & rs!DocType & "," & rs!DocTypeID & "," & rs!DateReceived & "," & rs!LinkToFile & "," & rs!Comments & ";"
rs.MoveNext
Loop
Now regarding your error. "Item not found in this collection" - could be because of the fact you were using the other fields which were not part of the recordset object. Try this code. Good luck. :)
I have an issue that has developed while modifying an update event that previously had been working. I am now getting a Runtime Error 3144, "Syntax error in UPDATE statement." When I go to debug the following line of code is flagged.
Set qdf = db.CreateQueryDef(vbNullString, strUpdate)
This function previously was working as it it was meant to in creating a SQL string to run an Update command. However I needed to adapt this same function to a different but mostly similar form.
I have included the complete code below for review and could use some assistance in spotting whatever little detail I missed/messed up in the transfer.
Private Sub btnEntEdt_Click()
Dim strUpdate As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
strUpdate = "UPDATE EntList AS e" & vbCrLf & _
"SET e.BusinessUnit = pBusinessUnit, " & _
"e.EntityID = pEntityID, " & vbCrLf & _
"e.EntityName = pEntityName, " & vbCrLf & _
"e.Location = pLoc, " & vbCrLf & _
"e.Client = pCli, " & vbCrLf & _
"e.Dept = pDept, " & vbCrLf & _
"WHERE e.EntityID = pEntityID;"
Debug.Print strUpdate
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strUpdate)
qdf.Parameters("pBusinessUnit") = Me.cboBUnit.Value
qdf.Parameters("pEntityName") = Me.txtEntName.Value
qdf.Parameters("pEntityID") = Me.txtEntID.Value
qdf.Parameters("pLoc") = Me.cboLoc.Value
qdf.Parameters("pCli") = Me.cboClient.Value
qdf.Parameters("pDept") = Me.cboDept.Value
qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing
Me.lstEntName.Requery
End Sub
You were wise to include Debug.Print strUpdate. Examine its output ...
UPDATE EntList AS e
SET e.BusinessUnit = pBusinessUnit, e.EntityID = pEntityID,
e.EntityName = pEntityName,
e.Location = pLoc,
e.Client = pCli,
e.Dept = pDept,
WHERE e.EntityID = pEntityID;
That statement triggers an error because of the comma at the end of the SET clause.
e.Dept = pDept,
^ here
Eliminate that comma, test the revised UPDATE statement in the query designer, and once you have it working correctly, modify your VBA code to produce the same statement text.
Or you could save the working version as a named query, qryUpdateEntList, and then reference the named query from your VBA code instead of re-creating the statement text at run time.
Set qdf = db.QueryDefs("qryUpdateEntList")
Apart from the syntax error, this combination looks wrong to me ...
SET e.EntityID = pEntityID
WHERE e.EntityID = pEntityID
Because of the WHERE clause, the UPDATE will only affect rows where EntityID = pEntityID. So there is no need to SET EntityID = pEntityID; they are already equal.
I want to apply an SQL query to one Access table, from which it is retrieving data from a table in another Access file. I've looked around on this subject and can't seem to get solutions to work.
Based on this source http://support.microsoft.com/kb/113701, I came up with the following, but still have no luck.
sSQL = "UPDATE TableInCurrentDB
SET [Field1InCurrentDB]= DAvg('Field1InExternalDB','[;database=C:\VB\ExternalDB.accdb].[TableInExternalDB]','Field2InExternalDB= & Year(Now()) & ')
WHERE [Field2InCurrentDB]='1';"
DoCmd.RunSQL sSQL
I know that the error lies somewhere in the reference to the external DB, because the code works fine if the tables are in the same database. However, it's tough to tell exactly what's wrong because the error I get is 'Unknown'.
How can I modify this statement to update an Access table from another Access database's table?
You prefer not to use a link to the table in the external database, but that choice is a complication when you want to use DAvg. However, since you're doing this with VBA code, you can ditch DAvg and do what you need in 2 steps:
First retrieve the average from the external table.
Use that step #1 average in your UPDATE.
For step #1, test this as a new query in the Access query designer ...
SELECT Avg(Field1InExternalDB)
FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'
WHERE Field2InExternalDB=Year(Date());
Assuming that query returns the correct value, adapt your VBA code to retrieve the same value.
Dim db As DAO.database
Dim strSelect As String
Dim varAvg As Variant
strSelect = "SELECT Avg(Field1InExternalDB)" & vbCrLf & _
"FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'" & vbCrLf & _
"WHERE Field2InExternalDB=Year(Date());"
'Debug.Print strSelect
Set db = CurrentDb
varAvg = db.OpenRecordset(strSelect)(0)
Debug.Print Nz(varAvg, 0) ' see note
Note that query will return Null when no rows include Field2InExternalDB values which match the current year. That is why varAvg is declared as Variant. Later Nz(varAvg, 0) will give you zero instead of Null.
Then you can use a parameter query for your UPDATE and supply Nz(varAvg, 0) as the parameter value.
Dim qdf As DAO.QueryDef
Dim strUpdate As String
strUpdate = "UPDATE TableInCurrentDB" & vbCrLf & _
"SET [Field1InCurrentDB]=[pAvg]" & vbCrLf & _
"WHERE [Field2InCurrentDB]='1';"
'Debug.Print strUpdate
Set qdf = db.CreateQueryDef(vbNullString, strUpdate)
qdf.Parameters("pAvg") = Nz(varAvg, 0)
qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing
Could you not do this as a single step? Incorporate the output of the first SQL as the input to the "set" in the second?
In other words,bypass the first query and just do the second using this as the "strUpdate" string:
strUpdate = "UPDATE TableInCurrentDB" & vbCrLf & _
"SET [Field1InCurrentDB]=" & vbCrLf & _
" (SELECT Val(Nz(Avg(Field1InExternalDB),0))" & vbCrLf & _
" FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'" & vbCrLf & _
" WHERE Field2InExternalDB=Year(Date()) )" & vbCrLf & _
"WHERE [Field2InCurrentDB]='1';"
I have created the code below to test whether I can run a query and retrieve a data from an SQL server table. And so far I can return the result using a MessageBox, but somehow I just don't know how to use this connection to update the table inside this Access file. Basically I want to use this as a front end file. Then, when the form is open it will automatically update the table inside this access file and load the data to the combo box as a list. I tried searching it here and read many discussions here and in Google but currently I can't find the right solution.
Option Compare Database
Sub LocalServerConn_Test()
Set conn = New adodb.Connection
Set rst = New adodb.Recordset
strDBName = "DataSet"
strConnectString = "Provider = SQLOLEDB.1; Integrated Security = SSPI; " & _
"Initial Catalog = " & strDBName & "; Persist Security Info = True; " & _
"Worksation ID = abc123;"
conn.ConnectionString = strConnectString
conn.Open
strSQL = "SELECT DISTINCT dbo.abc.abc123 FROM dbo.abc"
rst.Open Source:=strSQL, ActiveConnection:=strConnectString, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic
If rst.RecordCount = 0 Then
MsgBox "No records returned"
Else
rst.MoveFirst
Do While Not rst.EOF
MsgBox rst.Fields("abc123").Value
rst.MoveNext
Loop
End If
conn.Close
rst.Close
End Sub
You should be able to use code very similar to this:
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute _
"DELETE FROM LocalTable", _
dbFailOnError
cdb.Execute _
"INSERT INTO LocalTable (abc123) " & _
"SELECT DISTINCT abc123 " & _
"FROM " & _
"[" & _
"ODBC;" & _
"Driver={SQL Server};" & _
"Server=.\SQLEXPRESS;" & _
"Database=DataSet;" & _
"Trusted_Connection=yes;" & _
"].[dbo.abc]", _
dbFailOnError
Set cdb = Nothing
You can just keep the combo box bound to [LocalTable] and the updated values from the SQL Server table should appear.