Ben Adderson (10/5/2009)
RBarryYoung (9/30/2009)
So [ code="vb" ] has been removed then? Wow, that's a bummer.I have tried to replace it with [ code="other" ], unfortunately, "other" seems to mean C/C++/C#/Java or anything else that looks like C. It completely messes up the comment's for VB programs (because it treats them like a C/C++/C#/Java quotation mark). ... 🙁
Hi Barry,
I've just added [ code="vb" ] 🙂
I've updated my original post with an example, but I'm not very familiar with VB, so if you could give it a try and let us know about any issues, that would be great!
Yup, looks good.
For future reference, here is an example:
'APP_DataBase (vba)
'
' This is the module for the APP application's
'Database functions, routines and constants.
'
'2007-06-08 B.Young Created
'
Option Explicit
Public appDB As New ADODB.Connection
Public Function IsOpenDB() As Boolean
' function to track the DB status
IsOpenDB = (appDB.State <> 0) ' 0=closed
End Function
Public Sub CloseDB()
'Close the database if it is open.
If IsOpenDB Then appDB.Close
End Sub
Public Function OpenDB() As Boolean
'Open the Database and indicate if sucessful
Static sPrev As String
Dim sFile As String
If IsOpenDB Then
OpenDB = True 'we are already open
Exit Function
End If
If sPrev = "" Then sPrev = GetSetting(APP_AppName, "History", "DBName")
With Application.FileDialog(msoFileDialogFilePicker)
'specify the file open dialog
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Access Workbooks", "*.mdb"
.Filters.Add "All Files", "*.*"
.InitialFileName = sPrev
.Title = "Open TIP Database"
.Show
If .SelectedItems.Count > 0 Then
sFile = .SelectedItems(1)
Else 'user canceled ...
OpenDB = False
Exit Function
End If
End With
'appDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\byoung\APP\Main.mdb"
appDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFile
On Error Resume Next
appDB.Open
If Err.Number <> 0 Then
MsgBox "Error(" & Err.Number & "): " & Err.Description, vbOKOnly + vbCritical, "Error in OpenDB"
OpenDB = False
Exit Function
End If
'Opened ok, so finsh-up and exit
OpenDB = True
sPrev = sFile
SaveSetting APP_AppName, "History", "DBName", sPrev
End Function
Public Function DBLookup(SQLCommand As String, Optional DefaultVal As Variant = "") As Variant
'Execute a SQL statement and return a single value.
Dim rs As Recordset
On Error GoTo ErrHandler 'Handle any/all DB errors
If Not IsOpenDB Then OpenDB
' execute the command
Set rs = appDB.Execute(SQLCommand)
' extract the first field of the first record returned
rs.MoveFirst
DBLookup = rs.Fields(0).Value
Exit Function
ErrHandler:
' whatever the error is, just assign the default value and return
DBLookup = DefaultVal
Exit Function
End Function
Public Sub DBExecute(SQLCommand As String, Optional RecordsAffected As Long = 0)
'Execute a SQL statement that returns no values.
If Not IsOpenDB Then OpenDB
appDB.Execute SQLCommand, RecordsAffected
End Sub
'===================
Thanks!
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]