VB to .Net


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 OLEDB:Database 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.
 
Ad

Advertisements

T

Tom Shelton

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 OLEDB:Database 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.
You can create dll's in VB.NET - there is no need to put all of your code in a
form :).

As for the rest - you'll want to abandon the ADODB stuff, and look
into using OleDbDataProvider in the .NET framework....

For registry access, look at Microsoft.Win32. Here is a quick little example
of using the registry classes:

Option Strict On
Option Explicit On

Imports System
Imports Microsoft.Win32

Module Module1

Sub Main()
Using key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETFramework", False)
If key IsNot Nothing Then
Dim sPath As String = TryCast(key.GetValue("InstallRoot"), String)
If sPath IsNot Nothing Then
Console.WriteLine(sPath)
Else
Console.WriteLine("no value")
End If
Else
Console.WriteLine("no key")
End If
End Using
End Sub
End Module

HTH
 
M

Martin H.

Hello Bob,

ich you want to convert a VB6 project, just open it as a normal project
from the file menu in VB.NET.

The registry can be found under

My.Computer.Registry

The path you can get this way:
including the file name: Application.ExecutablePath

only the Directory: Dim MyPath As String = _
New System.IO.FileInfo(Application.ExecutablePath).Directory.FullName)

To access an Access database you can use ADO which is not .NET, but a
COM object (so you need to add the reference from the "COM" tab).

And to check your help, just press F1... ;)

Best wishes,

Martin
 
M

Michael Cole

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)
No it isn't. Mainly because it is a rewrite, not an upgrade. VB6 and
VB.Net, whilst they have similar syntax, are two completely different
languages.

[SNIP]
The above works Perfectly in VB 6.0
Because it is VB6 code
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)
Because it isn't VB.Net code. The code probably wouldn't work either if you
tried to open in in a Fortran or Pascal compiler. I don't mean to sound
harsh here, and yes, the learning curve from VB6 to VB.Net is a lot easier
than from VB6 to a non-VB syntaxed language, but don't think that you can
drop in a VB6 module and have it work. Even if it does compile fully, it
may operate differently, and it won't tell you this. Determinalistic
Finalisation versus Garbage Collection is a very big "gotcha", and boolean
operations have also changed.

Effectively, you need to write the program again.
 
C

Cor Ligthert[MVP]

Robert,

Some things to overthink

All version of VB share the same program language.
Like natural language some new words will everytijme be introduced and some
are slowly dying or even are not used any more.
One of the main goal from OOP (for me the most important) is to create good
maintanable code
OOP is mainly based on programming with objects (not as some think only on
creating OO dastaclasses although that technique is full usable)
Everything in VB.Net is inherited from the base class "Object" even as that
class is used as module (shared)
Be aware that most things are done by "a" reference, which are past by the
default by value of that reference. You are not passing complete objects.
VB is every version less depended from Win32 (Api's), however people used to
those keep mostly using those a long time (You saw that in past with guys
like Tom, but that has changed now).That is why you see everytime newer
frameworks, which are in fact OS extentions.
You can create in VB different DLL's however there is no need for that. You
can create classes and instance them in your program without that it are
DLL's (You can create a DLL by adding Dynamic Link Librarys as project to
your solution, which mainly is used in large solutions or in those
situantions where you have your own library beside librarys as System.Data
etc).
You need to convert your VB6 solution to VB9, however there is no need that
you direct remove your AdoDB, your API's etc.
A little bit frustrating can be, that the names of value types are now for a
32bit computer for better value types. However, API's use the old ones, so
their you have to look for. A long is in my idea best to translate to Int32
by instance.
As you are used to it, you will recognise that the Catch,Try,Finaly blocks
are easier maintainable then the OnError, I would change that in your
situation.
And as you want to do it right; set then in top of your program Option
Strict On, however, keep in mind that this feature is special for conversion
from VB6 to the newer types to keep the late binding possible. Late binding
is why VB6 is slower than by instance C#. Programs made by VB.Net with
Option strict on run at the same speed as every other program made with a
Net program language (managed code programs)

Just some things to overthink from my opinion.

Cor
 
R

Robert

Thanks to all that responed.
I have started to change the code as the suggestion where given.

I apprecieate your time, and hope that I can call on you all in the
future to pont me in the right direction on my upgrading to .Net.

Thanks again, and have a wonderful day.
Robert
 
Ad

Advertisements

A

Armin Zingler

Martin said:
Hello Bob,

ich you want to convert a VB6 project, just open it as a normal
project from the file menu in VB.NET.

The registry can be found under

My.Computer.Registry
Or, if you don't like My.Crap, you can go the straight way and use
Microsoft.Win32.Registry.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB .net 7
vb .net 1
vb,net 3
VB SCript in .NET? 9
C# to VB .NET 1
VB and .NET Framework 5
Datagrid VB net 4
VB Collections in .NET 4

Top