R
Robert
Sorry for the long post.
The following code snippets are from a current app I have in use, that I
created in Visual Basic 6.0 Enterprise.
I am currently trying to upgrade myself to the .Net world. (not that simple)
First. The following is from the dll I have created.
Option Explicit
'Make sure Microsoft ActiveX Data Objects 2.7 Library is added to the
References.
Dim cnExpenses As ADODB.Connection
Private Sub Class_Initialize()
'Find the Path.
Dim strPath As String
'Get Database Location.
Dim szBuffer As String, dataBuff As String, ldataBuffSize As Long, _
hKey As Long, phkResult As Long, retval As Long, _
Value As String, RegEnumIndex As Long
'Create Buffer
dataBuff = Space(255)
ldataBuffSize = Len(dataBuff)
'Find installed location of database.
szBuffer = "SOFTWARE\RJ Enterprize\Finance06"
hKey = HKEY_LOCAL_MACHINE
retval = RegOpenKeyEx(hKey, szBuffer, 0, KEY_ALL_ACCESS, phkResult)
Value = "Path"
retval = RegQueryValueEx(phkResult, Value, 0, 0, dataBuff,
ldataBuffSize)
strPath = Left(dataBuff, ldataBuffSize - 1)
'Close the keys
RegCloseKey hKey
RegCloseKey phkResult
Set cnExpenses = New ADODB.Connection
'Establish the Connection.
With cnExpenses
.Provider = "Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath &
";Mode=Read|Write;Jet OLEDBatabase Password=left out;"
.Open
End With
End Sub
Private Sub Class_Terminate()
'Close as set to Nothing.
cnExpenses.Close
Set cnExpenses = Nothing
End Sub
'Function to add record to DataBase.
Public Function AddExpenses(FixedID As String, Expense As String, EMnth As
Currency, EYr As Currency, _
Notes As String, Yr As String, Mnth As String,
SDay As String, _
MnthNo As String, CountID As String, UserCrted
As String, DteCrted As Date) As Boolean
Dim strSQL As String
On Error GoTo AddErr
strSQL = "INSERT INTO Expenses(FixedID, Expense, EMnth, EYr, Notes, Yr,
" & _
"Mnth, SDay, MnthNo, CountID, UserCrted, DteCrted)"
strSQL = strSQL & " VALUES"
strSQL = strSQL & "('" & FixedID & "',"
strSQL = strSQL & "'" & Replace(Expense, "'", "''") & "',"
strSQL = strSQL & "'" & EMnth & "',"
strSQL = strSQL & "'" & EYr & "',"
strSQL = strSQL & "'" & Replace(Notes, "'", "''") & "',"
strSQL = strSQL & "'" & Yr & "',"
strSQL = strSQL & "'" & Mnth & "',"
strSQL = strSQL & "'" & SDay & "',"
strSQL = strSQL & "'" & MnthNo & "',"
strSQL = strSQL & "'" & CountID & "',"
strSQL = strSQL & "'" & Replace(UserCrted, "'", "''") & "',"
strSQL = strSQL & "'" & DteCrted & "')"
cnExpenses.Execute strSQL
AddExpenses = True
Exit Function
AddErr:
MsgBox Err.Number & " - " & Err.Description
AddExpenses = False
Err.Clear
End Function
'======================
'Now the code from the Form Module that is called when I leave the
"Validating Function" to make sure all data is correct.
'Create a Reference.
Dim rsExpenses As ADODB.Recordset
Dim objExpenses As Finance06DLL.cExpenses
'Expenses.
Public Function SaveExp()
'frmFinance.
'Save the New Expenses Data.
On Error GoTo SaveErr
Set rsExpenses = New ADODB.Recordset
Set objExpenses = New Finance06DLL.cExpenses
'Open the Recordset.
With rsExpenses
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
End With
Dim blnRetVal As Boolean
With frmFinance
blnRetVal = objExpenses.AddExpenses(.lblItemID, .cboFixed.Text,
..txtFixed.Text, .lblFixed(3).Caption, _
.rtbFixed.TextRTF, .lblYr.Caption,
..lblMnth.Caption, .lblDay.Caption, _
.lblMnthNo.Caption, "1",
..stBar1.Panels(8).Text , Format(Now, "mmm dd, yyyy hh:nn:ss AMPM"))
End With
If blnRetVal = False Then
MsgBox "An Error Occured while Saving New Expenses Data to the
DataBase!", vbInformation, App.EXEName & " - Save Error"
Set rsExpenses = Nothing
Set objExpenses = Nothing
Exit Function
End If
Set rsExpenses = Nothing
Set objExpenses = Nothing
frmFinance.cboFixed.SetFocus
Exit Function
SaveErr:
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End Function
The above works Perfectly in VB 6.0
Tried to copy above codes to VB.Net 2008 Standard and nothing works.
Any samples how to open the registry, get the Path of the installation
directory. (this is created when the program is installed by the user)
Do I need to create dll's in .net or just place all the code in a form?
Any help on how to save, delete, load to and from an Access Database would
be very helpful.
Would use SQL Express, but I have not had time to learn it either.
The following code snippets are from a current app I have in use, that I
created in Visual Basic 6.0 Enterprise.
I am currently trying to upgrade myself to the .Net world. (not that simple)
First. The following is from the dll I have created.
Option Explicit
'Make sure Microsoft ActiveX Data Objects 2.7 Library is added to the
References.
Dim cnExpenses As ADODB.Connection
Private Sub Class_Initialize()
'Find the Path.
Dim strPath As String
'Get Database Location.
Dim szBuffer As String, dataBuff As String, ldataBuffSize As Long, _
hKey As Long, phkResult As Long, retval As Long, _
Value As String, RegEnumIndex As Long
'Create Buffer
dataBuff = Space(255)
ldataBuffSize = Len(dataBuff)
'Find installed location of database.
szBuffer = "SOFTWARE\RJ Enterprize\Finance06"
hKey = HKEY_LOCAL_MACHINE
retval = RegOpenKeyEx(hKey, szBuffer, 0, KEY_ALL_ACCESS, phkResult)
Value = "Path"
retval = RegQueryValueEx(phkResult, Value, 0, 0, dataBuff,
ldataBuffSize)
strPath = Left(dataBuff, ldataBuffSize - 1)
'Close the keys
RegCloseKey hKey
RegCloseKey phkResult
Set cnExpenses = New ADODB.Connection
'Establish the Connection.
With cnExpenses
.Provider = "Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath &
";Mode=Read|Write;Jet OLEDBatabase Password=left out;"
.Open
End With
End Sub
Private Sub Class_Terminate()
'Close as set to Nothing.
cnExpenses.Close
Set cnExpenses = Nothing
End Sub
'Function to add record to DataBase.
Public Function AddExpenses(FixedID As String, Expense As String, EMnth As
Currency, EYr As Currency, _
Notes As String, Yr As String, Mnth As String,
SDay As String, _
MnthNo As String, CountID As String, UserCrted
As String, DteCrted As Date) As Boolean
Dim strSQL As String
On Error GoTo AddErr
strSQL = "INSERT INTO Expenses(FixedID, Expense, EMnth, EYr, Notes, Yr,
" & _
"Mnth, SDay, MnthNo, CountID, UserCrted, DteCrted)"
strSQL = strSQL & " VALUES"
strSQL = strSQL & "('" & FixedID & "',"
strSQL = strSQL & "'" & Replace(Expense, "'", "''") & "',"
strSQL = strSQL & "'" & EMnth & "',"
strSQL = strSQL & "'" & EYr & "',"
strSQL = strSQL & "'" & Replace(Notes, "'", "''") & "',"
strSQL = strSQL & "'" & Yr & "',"
strSQL = strSQL & "'" & Mnth & "',"
strSQL = strSQL & "'" & SDay & "',"
strSQL = strSQL & "'" & MnthNo & "',"
strSQL = strSQL & "'" & CountID & "',"
strSQL = strSQL & "'" & Replace(UserCrted, "'", "''") & "',"
strSQL = strSQL & "'" & DteCrted & "')"
cnExpenses.Execute strSQL
AddExpenses = True
Exit Function
AddErr:
MsgBox Err.Number & " - " & Err.Description
AddExpenses = False
Err.Clear
End Function
'======================
'Now the code from the Form Module that is called when I leave the
"Validating Function" to make sure all data is correct.
'Create a Reference.
Dim rsExpenses As ADODB.Recordset
Dim objExpenses As Finance06DLL.cExpenses
'Expenses.
Public Function SaveExp()
'frmFinance.
'Save the New Expenses Data.
On Error GoTo SaveErr
Set rsExpenses = New ADODB.Recordset
Set objExpenses = New Finance06DLL.cExpenses
'Open the Recordset.
With rsExpenses
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
End With
Dim blnRetVal As Boolean
With frmFinance
blnRetVal = objExpenses.AddExpenses(.lblItemID, .cboFixed.Text,
..txtFixed.Text, .lblFixed(3).Caption, _
.rtbFixed.TextRTF, .lblYr.Caption,
..lblMnth.Caption, .lblDay.Caption, _
.lblMnthNo.Caption, "1",
..stBar1.Panels(8).Text , Format(Now, "mmm dd, yyyy hh:nn:ss AMPM"))
End With
If blnRetVal = False Then
MsgBox "An Error Occured while Saving New Expenses Data to the
DataBase!", vbInformation, App.EXEName & " - Save Error"
Set rsExpenses = Nothing
Set objExpenses = Nothing
Exit Function
End If
Set rsExpenses = Nothing
Set objExpenses = Nothing
frmFinance.cboFixed.SetFocus
Exit Function
SaveErr:
MsgBox Err.Number & " - " & Err.Description
Err.Clear
End Function
The above works Perfectly in VB 6.0
Tried to copy above codes to VB.Net 2008 Standard and nothing works.
Any samples how to open the registry, get the Path of the installation
directory. (this is created when the program is installed by the user)
Do I need to create dll's in .net or just place all the code in a form?
Any help on how to save, delete, load to and from an Access Database would
be very helpful.
Would use SQL Express, but I have not had time to learn it either.