Using Module

Hello, I am working on a program that uses a lot of subs and functions, so I wanted to bring those together in 1 single module file.
Now I did this (see code), but I noticed that it compiles the module with my app, and it doesn't compile it to an dll as I want to.
Anyone here knows how to solve this because it is important that I can 'update' the file just by replacing my module dll by a new one.
Here is the code I'm using:

Module modules
Public Sub SetSettings()

E
nd Sub
Public Sub GetSettings()

End Sub
Public Sub cfu(ByVal flag As Integer)

Select Case flag
Case 0
Try
My.Computer.Network.DownloadFile(My.Settings.updsite & "/updvers.txt", Application.StartupPath & "updvers.txt", "", "", False, 250, True)
With My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "updvers.txt")
If Not .ReadLine = My.Application.Info.Version.ToString Then
My.Computer.Network.DownloadFile(My.Settings.updsite & "/modules.bas", Application.StartupPath & "modules.bas", "", "", False, 250, True)
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "updvers.txt", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
Process.Start(Application.StartupPath &
"rs.exe")
Try
main.Close()
settings_app.Close()
edit_patients.Close()
add_patients.Close()
wmp_app.Close()
teeths.Close()
details.Close()
about.Close()
start_invisible.Close()
Catch ex As Exception
MsgBox(
"An error occured while closing " & My.Application.Info.ProductName & ":" & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
For Each proc As Process In System.Diagnostics.Process.GetProcesses
If proc.ProcessName = My.Application.Info.ProductName & ".exe" Then
proc.Kill()
End If
Next
End Try
End If
End With
Catch ex As Exception
If MsgBox("An error occured while updating " & My.Application.Info.ProductName & vbCrLf & "If this isn't the first time, please take note of the following error and send it to: " & My.Settings.supportmail & vbCrLf & "The error is:" & vbCrLf & ex.ToString & vbCrLf & vbCrLf & "Do you want to save this error to a textfile ", MsgBoxStyle.YesNo Or MsgBoxStyle.Critical, "Error occured") = MsgBoxResult.Yes Then
Try
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "error.txt", ex.ToString, False)MsgBox("The file has succesfully been saved" & vbCrLf & "The file name is:" & vbCrLf & "error.txt" & vbCrLf & "and can be found in " & Application.StartupPath, MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "File Saved")
Catch ax As Exception
MsgBox(
"Unable to save error to textfile", MsgBoxStyle.OkOnly, "Error")
End Try
End If
End Try
Case 1
Try
M
y.Computer.Network.DownloadFile(My.Settings.updsite & "/updvers.txt", Application.StartupPath & "updvers.txt", "", "", False, 250, True)
With My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "updvers.txt")
If Not .ReadLine = My.Application.Info.Version.ToString Then
If MsgBox("New updates are available for " & My.Application.Info.ProductName & vbCrLf & "Do you want to update now ", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "New updates available") = MsgBoxResult.Yes Then
T
ry
My.Computer.Network.DownloadFile(My.Settings.updsite & "/modules.bas", Application.StartupPath & "modules.bas", "", "", False, 250, True)
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "updvers.txt", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
Process.Start(Application.StartupPath &
"rs.exe")
Try
main.Close()
ettings_app.Close()
edit_patients.Close()
add_patients.Close()
wmp_app.Close()
teeths.Close()
details.Close()
about.Close()
start_invisible.Close()
Catch ex As Exception
MsgBox(
"An error occured while closing TandPerfect:" & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
For Each proc As Process In System.Diagnostics.Process.GetProcesses
If proc.ProcessName = "tandperfect.exe" Then
proc.Kill()
End If
Next
End Try
atch ex As Exception
End Try
End If
End If
End With
Catch ex As Exception
If MsgBox("An error occured while updating " & My.Application.Info.ProductName & vbCrLf & "If this isn't the first time, please take note of the following error and send it to: " & My.Settings.supportmail & vbCrLf & "The error is:" & vbCrLf & ex.ToString & vbCrLf & vbCrLf & "Do you want to save this error to a textfile ", MsgBoxStyle.YesNo Or MsgBoxStyle.Critical, "Error occured") = MsgBoxResult.Yes Then
Try
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "error.txt", ex.ToString, False)
MsgBox(
"The file has succesfully been saved" & vbCrLf & "The file name is:" & vbCrLf & "error.txt" & vbCrLf & "and can be found in " & Application.StartupPath, MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "File Saved")
Catch ax As Exception
MsgBox(
"Unable to save error to textfile", MsgBoxStyle.OkOnly, "Error")
End Try
End If
End Try
ase 2
Try
My.Computer.Network.DownloadFile(My.Settings.updsite & "/updvers.txt", Application.StartupPath & "updvers.txt", "", "", False, 250, True)
With My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "updvers.txt")
If
.ReadLine = "Prior" Then
Try
My.Computer.Network.DownloadFile(My.Settings.updsite & "/updvers.txt", Application.StartupPath & "updvers.txt", "", "", False, 250, True)
With My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "updvers.txt")
If Not .ReadLine = My.Application.Info.Version.ToString Then
My.Computer.Network.DownloadFile(My.Settings.updsite & "/modules.bas", Application.StartupPath & "modules.bas", "", "", False, 250, True)
My.Computer.FileSystem.DeleteFile(Application.StartupPath & "updvers.txt", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
Process.Start(Application.StartupPath &
"rs.exe")
Try
main.Close()
settings_app.Close()
edit_patients.Close()
dd_patients.Close()
wmp_app.Close()
teeths.Close()
details.Close()
about.Close()
start_invisible.Close()
Catch ex As Exception
MsgBox(
"An error occured while closing TandPerfect:" & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
For Each proc As Process In System.Diagnostics.Process.GetProcesses
If proc.ProcessName = "tandperfect.exe" Then
roc.Kill()
End If
Next
nd Try
End If
End With
Catch ex As Exception
If MsgBox("An error occured while updating " & My.Application.Info.ProductName & vbCrLf & "If this isn't the first time, please take note of the following error and send it to: " & My.Settings.supportmail & vbCrLf & "The error is:" & vbCrLf & ex.ToString & vbCrLf & vbCrLf & "Do you want to save this error to a textfile ", MsgBoxStyle.YesNo Or MsgBoxStyle.Critical, "Error occured") = MsgBoxResult.Yes Then
Try
M
y.Computer.FileSystem.WriteAllText(Application.StartupPath & "error.txt", ex.ToString, False)MsgBox("The file has succesfully been saved" & vbCrLf & "The file name is:" & vbCrLf & "error.txt" & vbCrLf & "and can be found in " & Application.StartupPath, MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "File Saved")
Catch ax As Exception
MsgBox(
"Unable to save error to textfile", MsgBoxStyle.OkOnly, "Error")
End Try
End If
End Try
End If
End With
atch ax As Exception
End Try
End Select
End Sub
Public Sub cs(ByVal sender As String)
Select Case sender
Case "main"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_common.dat")
Case "media"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_media.dat")
Case "add"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_adding.dat")
Case "edit"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_editing.dat")
Case "delete"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_deleting.dat")
Case "settings"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_settings.dat")
Case "details"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_details.dat")
Case "drawing"
help.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("h_drawing.dat")
End Select
help.Show()
End Sub
Public Sub savelog(ByVal msg As String)
Try
My.Computer.FileSystem.WriteAllText("log.txt", Now & msg & vbCrLf, True)
Catch ex As Exception
MsgBox(
"An error occured while saving to log file", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error occured")
End Try
E
nd Sub
Public Sub InCorrectDate()
End Sub
Public Sub save2db()
Dim blnexists As Boolean = False
For Each poss As patientsDSx.patients_1Row In main.PatientsDSx.patients_1.Rows
If poss.LastName = add_patients.txtname.Text Then
If poss.FirstName = add_patients.txtfname.Text Then
If poss.RegNr = add_patients.txtregnr.Text Then
blnexists = True
nd If
E
nd If
nd If
Next
If blnexists = False Then
add_patients.lblstatus_2.Text = "Adding"
add_patients.ProgressBar1.Value = 5
add_patients.txtlastvisit.Text = Format(TimeOfDay,
"hh:mm:ss tt")
add_patients.ProgressBar1.Value = 10
Dim id As Integer = (main.DataGridViewx.Rows.GetLastRow(DataGridViewElementStates.None) + 1)
Dim row As patientsDSx.patients_1Row = main.PatientsDSx.patients_1.Newpatients_1Row()
add_patients.ProgressBar1.Value = 15
row.LastName = add_patients.txtname.Text
add_patients.ProgressBar1.Value = 20
row.FirstName = add_patients.txtfname.Text
add_patients.ProgressBar1.Value = 25
row.BirthDate = add_patients.txtbd.Text
add_patients.ProgressBar1.Value = 30
row.Address = add_patients.txtaddress.Text
add_patients.ProgressBar1.Value = 35
row.ZIP = add_patients.txtzip.Text
add_patients.ProgressBar1.Value = 40
row.City = add_patients.txtcity.Text
add_patients.ProgressBar1.Value = 45
row.TelNr = add_patients.txttelnr.Text
add_patients.ProgressBar1.Value = 50
row.GSMNr = add_patients.txtgsmnr.Text
add_patients.ProgressBar1.Value = 55
row.BloodGroup = add_patients.cbbloudgroup.SelectedText
add_patients.ProgressBar1.Value = 60
row.Notes = add_patients.txtnotes.Text
add_patients.ProgressBar1.Value = 65
row.Doctor = add_patients.txtdoctor.Text
add_patients.ProgressBar1.Value = 70
row.HIF = add_patients.txthif.Text
add_patients.ProgressBar1.Value = 75
row.RegNr = add_patients.txtregnr.Text
add_patients.ProgressBar1.Value = 80
row.LastVisit = add_patients.txtlastvisit.Text
add_patients.ProgressBar1.Value = 85
row.Age = DateDiff(DateInterval.Year, row.BirthDate, Now.Date)
add_patients.ProgressBar1.Value = 90
main.PatientsDSx.patients_1.Rows.Add(row)
add_patients.ProgressBar1.Value = 95
main.PatientsDSx.AcceptChanges()
main.DataGridViewx.Refresh()
main.DataGridViewx.Update()
add_patients.ProgressBar1.Value = 100
add_patients.lblstatus_2.Text =
"Done"
Else
MsgBox("This patient already exists in the database", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Not added")
Exit Sub
End If
add_patients.Close()
End Sub
Public Function chkdate(ByVal date2c As String)
Dim booln As Boolean = False
If Not date2c.Length = 10 Then
A
dd_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
Exit Function
ElseIf Not 1 <= Mid(date2c, 1, 2) <= 31 Then
add_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
Exit Function
ElseIf Not Mid(date2c, 3, 1) = "/" Then
add_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
Exit Function
ElseIf Not 1 <= Mid(date2c, 4, 2) <= 12 Then
A
dd_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
Exit Function
ElseIf Not Mid(date2c, 6, 1) = "/" Then
add_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
E
xit Function
ElseIf Not 1 <= Mid(date2c, 7) <= Integer.MaxValue Then
add_patients.ErrorProvider1.SetError(add_patients.txtbd, "Please fill in a date in the correct format." & vbCrLf & "eg: 17/07/1990")
Exit Function
End If
booln = True
End Function
Public Function chkzip(ByVal zip As String)
Dim bln As Boolean = False
If Not add_patients.txtzip.Text.Length = 7 Then
add_patients.ErrorProvider2.SetError(add_patients.txtzip, "Please fill in a ZIP-code in the correct format" & vbCrLf & "For example: 3500-BE")
Exit Function
ElseIf Not Mid(zip, 5, 1) = "-" Then
add_patients.ErrorProvider2.SetError(add_patients.txtzip, "Please fill in a ZIP-code in the correct format" & vbCrLf & "For example: 3500-BE")
Exit Function
ElseIf Not "A" <= Mid(zip, 6, 1) <= "Z" Then
add_patients.ErrorProvider2.SetError(add_patients.txtzip, "Please fill in a ZIP-code in the correct format" & vbCrLf & "For example: 3500-BE")
Exit Function
ElseIf Not "A" <= Mid(zip, 7, 1) <= "Z" Then
add_patients.ErrorProvider2.SetError(add_patients.txtzip, "Please fill in a ZIP-code in the correct format" & vbCrLf & "For example: 3500-BE")
Exit Function
End If
bln = True
End Function
End
Module


Any help would be appreciated.
Grtz, Tom.



Answer this question

Using Module

  • CobyFernandess

    Thanks Renee,
    I've already tried that, but I don't know how the import option works.
    Can you tell me
    And also, when I cut and paste the code into a class project, it ig giving me 102 errors due to that my forms etc aren't declared.
    Any way to solve this too

    Grtz, Tom.



  • David M. Kean

    You can export your module functions and sub through a proxy class. Here's an example:

    in ClassLibrary1, the contents of your Module:
    Module Module1
    Public GlobalInt As Integer
    Public Sub GlobalSub()
    Debug.Print("Called through proxy")
    End Sub
    Public Function GlobalFunc() As Integer
    Debug.Print("Called through proxy")
    Return 0
    End Function
    End Module

    in ClassLibrary1, add a class to act as your proxy:
    Public Class Proxy
    Public Shared Property GlobalInt() As Integer
    Get
    Return Module1.GlobalInt
    End Get
    Set(ByVal value As Integer)
    Module1.GlobalInt = value
    End Set
    End Property
    Public Shared Sub GlobalSub()
    Module1.GlobalSub()
    End Sub
    Public Shared Function GlobalFunc() As Integer
    Return Module1.GlobalFunc
    End Function
    End Class

    Note how I used the Shared keyword, you'll never need to actually create an instance of the Proxy class. Compile ClassLibrary1 to create the DLL. Add a reference to this class library in your own .EXE project. Then:

    Imports ClassLibrary1.Proxy
    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer = GlobalInt
    i = GlobalFunc()
    GlobalSub()
    End Sub
    End Class

    The Imports statement ensures you can reference all the properties and methods in the DLL with simple names, just like the names you would use to reference the original variables and procedures in your old module.

    You can easily include forms in your DLL, just right-click the project, Add + Windows Form. You can use these forms in your .EXE:
    Dim f As ClassLibrary1.Form1
    f.Show()



  • G S

    tom...

    if you create an empty class library project

    Take out all of the code in Module between the Module declaration and End Module,

    you should be able to cut and paste that into a DLL. Don't forget your imports.



  • DeepDubey

    Lo Renee,
    can you please give a bit more info, because in my ClassLibrary, I can't declare a variable as 'Form', and I also can't import system.windows.forms.
    This is something my class should be able to do:

    Try
    main.Close()
    settings_app.Close()
    edit_patients.Close()
    add_patients.Close()
    wmp_app.Close()
    teeths.Close()
    details.Close()
    about.Close()
    start_invisible.Close()
    Catch ex As Exception
    MsgBox(
    "An error occured while closing " & name & ":" & vbCrLf & ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Error")
    For Each proc As Process In System.Diagnostics.Process.GetProcesses
    If proc.ProcessName = name & ".exe" Then
    proc.Kill()
    End If
    Next
    End Try

    If this isn't possible, please tell me so I can go thinking about another way to do this.



  • Winnie Ng - MS

    I don't know what you men by export in this case.

    uh-oh....

    You have references to forms

    Yes there is a way to deal with that which is to pass the form an argument:

    Instead of having your Dll reference Form1... which it cant do

    pass the form to the method as an argument.

    A(Me,ect)

    Private Sub A(byval F as Form, byval Etc as Integer)

    End Sub

    This should take care of that....



  • Using Module