Cross posted at:
https://www.mrexcel.com/board/member...y-mort.401365/
The reason is that it has been up there for a few days now and I can't get solution for it.
The variables
Option Explicit
Dim objFSO, objFile As Object
Dim objConnection, objRecordset As Object
Dim LastLocalChange, LastDbUpdate As Date
Dim DbFile, ConnString As String
This part of the code sync to the database:
Sub SyncToDatabase()
DbFile = Sheet1.Range("M5").Value 'Customer Database Location
LastLocalChange = Sheet1.Range("B12").Value
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error GoTo FileMissing
Set objFile = objFSO.GetFile(DbFile)
If objFile.DateLastModified < LastLocalChange Then 'Local Change was made, update Database
Kill (DbFile) 'Delete the current database version
ThisWorkbook.Sheets("CustDb").Copy
ActiveWorkbook.SaveAs DbFile, FileFormat:=51
ActiveWorkbook.Close False
End If
Exit Sub
FileMissing:
MsgBox "Please browse for the database file", vbInformation, "SlimSoft Systems"
BrowseForFile
End Sub
This part of the code sync from the database:
Sub SyncFromDatabase()
LastLocalChange = Sheet1.Range("B12").Value
DbFile = Sheet1.Range("M5").Value 'Customer Database Location
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error GoTo FileMissing
Set objFile = objFSO.GetFile(DbFile)
If objFile.DateLastModified > LastLocalChange Then 'Database Change was made, update Local Database
'Check Last Database Update
Sheet2.Range("A2:G9999").ClearContents 'Clear existing data
On Error Resume Next
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & _
Sheet1.Range("M5").Value & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=0"";"
objRecordset.Open "Select * FROM [CustDb$]", objConnection
Sheet2.Range("A2").CopyFromRecordset objRecordset
objRecordset.Close
objConnection.Close
On Error GoTo 0
End If
Exit Sub
FileMissing:
MsgBox "Please browse for the database file", vbInformation, "SlimSoft Systems"
BrowseForFile
End Sub
I still need help with this please
So I tried to run a few tests and what I saw was that when I added new sheets to the custData workbook and sync the data to the database, all the sheets are deleted and a new sheet named CustDb is created.
Is there a way I can, maybe delete only the CustDb sheet, update it with the new data and leave the other sheets undeleted?
Thanks for helping me out.
Bookmarks