Hi there,
I have a problem that is really causing me grief. I have a VB application that opens an ADO connection to a database. When the application is first installed, the database does not exist. Normally it will. For that reason, the code to create the database file (by copying it from a template stored as an application resource) is executed in response to an error opening the original connection.
My problem is that once the new DB file has been created, ADO fails to open the file. The problem is obviously timing related as stepping through the code in the IDE works 100% of the time! Can anyone suggest a mechanism to fix this without resorting to an arbitrary delay factor or similar?
Here is the current code (with some parts snipped) m_ConData is a module level ADO Connection object:
Public Property Let DBItem(NewItem as String)
Dim sConnect As String
Dim fDBCreated As Boolean
On Error Goto ErrorHandler
‘// Make sure fDBCreated is initialized.
fDBCreated = False
‘// Open the connection (if it isn’t)
If m_conData Is Nothing Then
sConnect = CONNECTION_STRING
Set m_conData = New Connection
With m_conData
.ConnectionString = sConnect
.Open
End With
End If
‘// Functionality removed here….
ExitPoint:
Exit Property
ErrorHandler:
Select Case Err
Case &H80004005 ‘ ADO Error – Probably because file does not exist.
If Not fDBCreated Then
If CopyResourceToFile Then
fDBCreated = True
Resume
Else
Err.Raise vbObjectError, Description:=”Unable to createor access DB.”
End If
End If
End Select
Resume ExitPoint
End Property