|
-
Nov 4th, 2000, 07:15 PM
#1
Thread Starter
New Member
What the heck are modules? After a year of VB6 programming my teacher never once mentioned modules... come to think of it, we didnt learn much, heh.
What's a good book that will teach me about data-types and modules and pretty much everything else VB?
Thanks a lot
I am l33t
I am a H4x0r
I am a l33t H4x0r
(j/k =) )
-
Nov 4th, 2000, 07:20 PM
#2
Hyperactive Member
Modules are for sharing information between forms, if you want to have some globals variables lets say, you put it in the module, and than all your forms for this project can use this variable.
Same thing for functions/subs, just put it in the module and all your forms can "see" and use it.
A good book:
VB6.0 in 21 days by SAMS
cheers
In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.
- Douglas Adams
The Hitchhiker's Guide to the Galaxy
-
Nov 4th, 2000, 07:24 PM
#3
PowerPoster
Modules simple answer
I use module to place code that really doesn't go with a spcific form ie :
Standard print command
API commands
Conversion formulas (sting to value,value to string)
Declaring global variables - which more than one form may have to "see" the value.
I have read opinions on this Module concept and some people feel it is wasted space. I think it simplifies and provides a better means of reusable code.
Just my thoughts...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Nov 4th, 2000, 08:34 PM
#4
Frenzied Member
Well, if you re-use code between various forms, put it in a module so it will (as stated by the other replies) be available for all forms.
I use it for some api calls but the most for global functions, a good programmer can't live without them!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Nov 4th, 2000, 09:42 PM
#5
Member
Also, modules are just text files. There is no graphical interface to them (i.e., not like forms). Strangly enough, you can give them names just like any other object, though....
-
Feb 11th, 2004, 08:35 AM
#6
Junior Member
I've been wondering about modules myself. I've been doing VB for two weeks now (lucky me!).
I have a few db queries that I would like to share between different forms, as well as different Subs within individual forms. Would these be good reasons to use modules?
I've tried to run a db query from a module, but I had no luck. I wasn't sure how to write the module out... Do I need to write it as a function or a Sub within the module? Any help would be appreciated.
Josh
-
Feb 11th, 2004, 09:01 AM
#7
Not NoteMe
Yes, you could write functions/subs that would perform the query on the database and perhaps output results somehow.
If you wanted to store the results you could declare a variable in the module, the variable would be 'seen' by all forms in your project.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Feb 11th, 2004, 09:08 AM
#8
Junior Member
Can you please give me a quick example? I think it's the variable setting part that's killing me. I can't figure out the syntax to assign a variable in the module, and then how to call that variable in a form to reference it.
What I'm trying to do is run a query to look up a tax percentage in a database table, assign that number to a variable, and then be able to do some basic math in a form by referencing that tax percentage variable.
Thanks,
Josh
-
Feb 11th, 2004, 09:20 AM
#9
Modules allow for a tidier code, you could put all the module code in your main form code, but by using the module, evrything becomes a bit more organised and easier to read. Your main code will link to the module code to look for a function when it comes up against something it doesnt understand or hasnt been declared.
-
Feb 11th, 2004, 09:26 AM
#10
Junior Member
I understand that part. In this particular case I would like to store the tax percentage in a module because it gets used in different places in my code. For example, I need to display the tax percentage (7.2%, for example) so the user knows what it is. I also need to use that percentage to come up with the total order price (subtotal multiplied by the tax). I figure that if I write the tax percentage query in a module, I can then assign the percentage to a variable in the module and then call that variable in several other places in my project.
The problem is, I don't know how to write the code to do it. I am hoping for a quick example so I can stop being stupid.
-
Feb 11th, 2004, 09:26 AM
#11
Not NoteMe
If my memory serves me correctly you use 'Global' instead of 'Dim' if you want to make a variable in a module that can be accessed by all forms.
Other than that it's all exactly the same as if it were in a form.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Feb 11th, 2004, 09:31 AM
#12
Junior Member
Okay, so let me see if this makes sense. Here is what I'm running in the module to get the percentage. ("db" is set in the main form I have.)
VB Code:
Sub Get_The_Damn_Tax_Already()
' Get the current tax rate from the lkpTax table and display it
Global Get_Tax As ADODB.Recordset
Set Get_Tax = New ADODB.Recordset
Get_Tax.Source = "Select Tax_Rate from lkpTax"
Set Get_Tax.ActiveConnection = db
Get_Tax.Open
Get_Tax.MoveFirst
End Sub
So, do I now have to create another Global variable (call it something like "display_perc") in the Sub? In other words:
VB Code:
Global display_perc = Get_Tax.Fields!Tax_Rate
Is that correct?
Josh
-
Feb 11th, 2004, 09:37 AM
#13
Addicted Member
Put the reserved word Public in front!
Public Sub ....
or
Public Function ...
if you want to access it from other forms or modules.
For your variable.... you have to put it in the declaration part of the module. (At the top, not in a sub or function).
Hope that help.
Mens sana in corpore sano
... pour mieux travailler!
-
Feb 11th, 2004, 09:39 AM
#14
Junior Member
Okay, did that. Now how to call the variable.
Man, am I a pain in the butt or what?
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 09:46 AM
#15
Addicted Member
Here is an exemple of a module
VB Code:
Option Explicit
Private Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type
Private Type SAFEARRAY2D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 1) As SAFEARRAYBOUND
End Type
Private Type BITMAP '14 bytes
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Public Const SRCPAINT = &HEE0086 ' (DWORD) dest = source OR dest
Public Const SRCINVERT = &H660046 ' (DWORD) dest = source XOR dest
Public Const SRCERASE = &H440328 ' (DWORD) dest = source AND (NOT dest )
Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Public Const SRCAND = &H8800C6 ' (DWORD) dest = source AND dest
Public Const DSTINVERT = &H550009 ' (DWORD) dest = (NOT dest)
Public Const NOTSRCCOPY = &H330008 ' (DWORD) dest = (NOT source)
Public Const NOTSRCERASE = &H1100A6 ' (DWORD) dest = (NOT src) AND (NOT dest)
Public Const MERGECOPY = &HC000CA ' (DWORD) dest = (source AND pattern)
Public Const MERGEPAINT = &HBB0226 ' (DWORD) dest = (NOT source) OR dest
Public Const PATCOPY = &HF00021 ' (DWORD) dest = pattern
Public Const PATINVERT = &H5A0049 ' (DWORD) dest = pattern XOR dest
Public Const PATPAINT = &HFB0A09 ' (DWORD) dest = DPSnoo
Public Const WHITENESS = &HFF0062 ' (DWORD) dest = WHITE
Public Const BLACKNESS = &H42 ' (DWORD) dest = BLACK
'Public myPicture As New StdPicture
Private Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Function Transform(ByVal myPicture As StdPicture, NewColor As Long, TransColor As Long) As StdPicture
Dim lMax As Long, lMid As Long, lMin As Long
Dim p As Integer, q As Integer, t As Integer, z As Integer
Dim lDifference As Single
Dim Hue As Single
Dim Luminance As Single
Dim Saturation As Single
Dim pic() As Byte
Dim sa As SAFEARRAY2D
Dim bmp As BITMAP
Dim R As Long, G As Long, B As Long
Dim transR As Long, transG As Long, transB As Long
Dim i As Integer, j As Integer
' the code here
Set Transform = myPicture
End Function
Public Function ReplaceColor(ByVal myPicture As StdPicture, OldColor As Long, NewColor As Long, Similar As Long) As StdPicture
Dim pic() As Byte
Dim sa As SAFEARRAY2D
Dim bmp As BITMAP
Dim R_New As Long, G_New As Long, B_New As Long
Dim R_Old As Long, G_Old As Long, B_Old As Long
Dim R_Min As Long, G_Min As Long, B_Min As Long
Dim R_Max As Long, G_Max As Long, B_Max As Long
Dim R As Long, G As Long, B As Long
Dim i As Integer, j As Integer
'your code here
End Function
Mens sana in corpore sano
... pour mieux travailler!
-
Feb 11th, 2004, 09:49 AM
#16
Junior Member
So after the "Transform" function, you are setting a variable called "transform" and that variable can be called outside of the module by doing somthing like this:
label_rate.Caption = Transform
Is that right? I feel like I'm missing something...
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 11:37 AM
#17
Junior Member
I just realized that I hijacked the thread. I apologize to Lost_Light; I hope you're not offended...
I changed my thinking a bit, but I'm still lost. I tried turning the module into a function. Here's what I have so far:
VB Code:
Global The_Damn_Tax_Rate As String
Public Function Get_The_Damn_Tax_Already()
' Get the current tax rate from the lkpTax table and display it
Dim Get_Tax As ADODB.Recordset
Set Get_Tax = New ADODB.Recordset
Get_Tax.Source = "Select Tax_Rate from lkpTax"
Set Get_Tax.ActiveConnection = db
Get_Tax.Open
Get_Tax.MoveFirst
Get_Tax.Close
Set The_Damn_Tax_Rate = Get_Tax.Fields!Tax_Rate
End Function
Then, in Sub Form_Load() on the form, I have this:
VB Code:
Call Module1.Get_The_Damn_Tax_Already
label_tax_percentage.Caption = The_Damn_Tax_Rate
I'm sure I'm not setting "The_Damn_Tax_Rate" correctly in either place.
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 12:20 PM
#18
Lively Member
See if this example and explanation will help you get your mind wrapped around using modules. They really are quite simple.
First .... MS recommends that you NEVER use GLOBAL variables. Security problem!!! Instead use Public. They can be seen anywhere in your project, but not by something outside of your app.
Here is some code that opens an Access Database from a module
then uses the recordset in a form.
VB Code:
'Module is called "modTestConnect"
Option Explicit
'The Connection and Recordset objects are visible to any procedure that calls them.
Public cnMyCon As ADODB.Connection 'Connection to ADODB
Public MyRS As ADODB.Recordset 'Holds records
Public Sub ConnectToDBase(strSQL As String, Optional blnDel As Boolean)
Dim lngRecordCount As Long
Set cnMyCon = New ADODB.Connection
Set MyRS = New ADODB.Recordset
MyRS.CursorLocation = adUseClient
cnMyCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test.mdb;Persist Security Info=False"
cnMyCon.CursorLocation = adUseClient
MyRS.Open strSQL, cnMyCon, adOpenDynamic, adLockOptimistic
If blnDel = True Then
Exit Sub
End If
If Not MyRS.EOF Or Not MyRS.BOF Then
MyRS.MoveLast 'without these the recordcount
MyRS.MoveFirst 'ends up -1
End If
lngRecordCount = MyRS.RecordCount
End Sub
Public Sub DisconnectDBase()
If MyRS.State = adStateOpen Then
MyRS.Close
End If
cnMyCon.Close
Set MyRS = Nothing
Set cnMyCon = Nothing
End Sub
'Form code
'Several procedures shown here
Private Sub MakeConnection() 'called from Form_Load event
Dim strSQL As String
Me.MousePointer = vbHourglass
'SQL statement that will be passed to module
strSQL = "SELECT CompanyName FROM Suppliers WHERE dropped = false;"
'calling the module and passing it the required input(strSQL)
modTestConnect.ConnectToDBase strSQL
'Other procedures
LoadControls ' <<< Notice this routine below. It uses the
'RecordSet created above
ResetOpt
'Other procedures that need this RecordSet are called BEFORE
'the RS is closed.
MyRS.Close
Me.MousePointer = vbNormal
End Sub
In the above we made a module to connect to the DBase, and have variables that can be seen by any form.
Another Routine in the same form that above code was taken from
VB Code:
Private Sub LoadControls() 'Loading Dynamic Controls
Dim i As Integer
Dim strName As String
Do Until MyRS.EOF '<< MyRS is Public in module' No need to
'Dim a variable for it.
'Using the recordset
strName = MyRS.Fields.Item("CompanyName").Value
Option1(i).Caption = strName
With Option1(i)
If i > 0 Then
.Top = Option1(i - 1).Top + Option1(i - 1).Height + 10
.Left = Option1(i - 1).Left
.Width = Frame1.Width - 500
End If
.Visible = True
.Value = False
End With
i = i + 1
MyRS.MoveNext
If Not MyRS.EOF Then
Load Option1(i)
End If
Loop
HTH
Michael
-
Feb 11th, 2004, 01:41 PM
#19
Junior Member
Michael,
I'm convinced that I'm the dumbest person on the planet. In the example you gave, you set a variable called "lngRecordCount." Is that one of the variables that you call from a form? If so, how do you actually use that variable in the form code?
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 01:43 PM
#20
You can't the function is using it...
-
Feb 11th, 2004, 01:45 PM
#21
Junior Member
Originally posted by NoteMe
You can't the function is using it...
I can't what? I'm lost...
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 01:51 PM
#22
I haven't read all the posts in this thread but the use of modules is very simple. Here is a trivial example of sharing a variable between two forms.
VB Code:
'In the module
Option Explicit
Public gintCount As Integer
' In Form1
Private Sub Form_Load()
gintCount = gintCount + 1
MsgBox gintCount ' Will display "1"
Form2.Show
End Sub
' In Form2
Private Sub Form_Load()
gintCount = gintCount + 1
MsgBox gintCount ' Will display "2"
End Sub
-
Feb 11th, 2004, 01:55 PM
#23
Junior Member
Okay, I'm going to try this a different way. Here is the code I have so far. If you could just tell me what is wrong (in other words, actually tell me what I need to change to make this work), I think that will be the best way for me to figure out why it's not working. I now understand the idea of how to use modules; I just don't understand why my particular code isn't working.
Here is the module:
VB Code:
Option Explicit
Public The_Damn_Tax_Rate As String
Public Sub Get_The_Damn_Tax_Already()
' Get the current tax rate from the lkpTax table and display it
Dim Get_Tax As ADODB.Recordset
Set Get_Tax = New ADODB.Recordset
Get_Tax.Source = "Select Tax_Rate from lkpTax"
Set Get_Tax.ActiveConnection = db
Get_Tax.Open
Get_Tax.MoveFirst
Set The_Damn_Tax_Rate = Get_Tax.Fields!Tax_Rate
Get_Tax.Close
End Sub
Here is the code I'm using on the form to try and use the variable I set in the module:
VB Code:
label_tax_percentage.Caption = Module1.The_Damn_Tax_Rate
If you can tell me what exactly it is that I'm doing wrong, that should end this thread (and my misery).
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 02:42 PM
#24
The Set in the following is wrong and you should remove it. Set is for objects and not simple variables. You should also replace the "!" with a ".".
VB Code:
Set The_Damn_Tax_Rate = Get_Tax.Fields!Tax_Rate
You don't need Module1 in the following since anything declared as Public in a module is known everywhere.
VB Code:
label_tax_percentage.Caption = Module1.The_Damn_Tax_Rate
-
Feb 11th, 2004, 02:57 PM
#25
Junior Member
I actually got it to work. For the life of me I don't know how, but here's what I did:
VB Code:
Option Explicit
Public dbTax As ADODB.Connection
Public Get_Tax As ADODB.Recordset
Public The_Damn_Tax_Rate As String
Public Sub Get_The_Damn_Tax_Already()
Set dbTax = New ADODB.Connection
Set Get_Tax = New ADODB.Recordset
dbTax.ConnectionString = "DSN=Mobile_Sales_App_Database;"
dbTax.Open
' Get the current tax rate from the lkpTax table
Get_Tax.Source = "Select Tax_Rate from lkpTax"
Set Get_Tax.ActiveConnection = dbTax
Get_Tax.Open
Get_Tax.MoveFirst
The_Damn_Tax_Rate = Get_Tax.Fields!Tax_Rate
End Sub
Public Sub DisconnectDBase()
If Get_Tax.State = adStateOpen Then
Get_Tax.Close
End If
dbTax.Close
Set Get_Tax = Nothing
Set dbTax = Nothing
End Sub
In the form I did this:
VB Code:
Call Get_The_Damn_Tax_Already
label_tax_percentage.Caption = The_Damn_Tax_Rate
I'm sure there is a more efficient way of doing it, but this will do for now.
Thanks to everyone for all of your help. I hope I wasn't too frustrating.
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
-
Feb 11th, 2004, 03:06 PM
#26
Hope this isn't confusing but you don't need your Public variable if you turn the sub into a function.
VB Code:
Option Explicit
Public dbTax As ADODB.Connection
Public Get_Tax As ADODB.Recordset
'Public The_Damn_Tax_Rate As String You don't need this
Public [b]Function [/b]Get_The_Damn_Tax_Already() [b]As String[/b]
Set dbTax = New ADODB.Connection
Set Get_Tax = New ADODB.Recordset
dbTax.ConnectionString = "DSN=Mobile_Sales_App_Database;"
dbTax.Open
' Get the current tax rate from the lkpTax table
Get_Tax.Source = "Select Tax_Rate from lkpTax"
Set Get_Tax.ActiveConnection = dbTax
Get_Tax.Open
Get_Tax.MoveFirst
'The_Damn_Tax_Rate = Get_Tax.Fields!Tax_Rate
[b]Get_The_Damn_Tax_Already = Get_Tax.Fields!Tax_Rate[/b]
End Function
Public Sub DisconnectDBase()
If Get_Tax.State = adStateOpen Then
Get_Tax.Close
End If
dbTax.Close
Set Get_Tax = Nothing
Set dbTax = Nothing
End Sub
'In the form
'Call Get_The_Damn_Tax_Already
'label_tax_percentage.Caption = Get_The_Damn_Tax_Already
[b]label_tax_percentage.Caption = Get_The_Damn_Tax_Already[/b]
-
Feb 11th, 2004, 03:14 PM
#27
Junior Member
That worked perfect, thanks. I think that was the one thing that was NOT confusing about this.
Thank you for all of your help.
Josh Sager
www.buyubooks.com
www.tlavideo.com
www.shinobi.org
"Ya know, it's much easier in ColdFusion!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|