Difference between revisions of "Creating Public and Private Functions in VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(New page: == Function Example == Private Function fnFormatPhoneNum(strIn As String) As String Dim strLen As Integer strIn = Trim(strIn): strLen = Len(strIn) Select Case strLen Case 1...)
 
Line 17: Line 17:
  
 
   strVariableName = fnFormatPhoneNum("" & arCompanies(cnt, 4))
 
   strVariableName = fnFormatPhoneNum("" & arCompanies(cnt, 4))
 +
 +
== Public Function in a Module ==
 +
 +
You may want to create a function that is available to all forms in your program.  You can put it in a module and make it public so that it may be used by any form.
 +
 +
MODULE CODE:
 +
 +
  Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
 +
    Dim KeyHand&
 +
    Dim datatype&
 +
    Call RegOpenKey(HKEY, sPath, KeyHand&)
 +
    GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
 +
    Call RegCloseKey(KeyHand&)
 +
  End Function
 +
 +
FORM CODE:
 +
 +
  strRetval = MODULE_NAME.GetSettingEx(HKEY_CURRENT_USER, "Software\album", "DefaultStorybookPath")
  
 
 
 
 

Revision as of 13:27, 9 January 2008

Function Example

Private Function fnFormatPhoneNum(strIn As String) As String
  Dim strLen As Integer
  strIn = Trim(strIn):   strLen = Len(strIn)
  Select Case strLen
  Case 10
    fnFormatPhoneNum = "(" & Right(strIn, 3) & ") " & Mid(strIn, 3, 3) & "-" & Left(strIn, 4)
  Case 7
    fnFormatPhoneNum = Right(strIn, 3) & "-" & Left(strIn, 4)
  Case Else
    fnFormatPhoneNum = strIn
  End Select
End Function

Here's how to call the function above from the program code:

 strVariableName = fnFormatPhoneNum("" & arCompanies(cnt, 4))

Public Function in a Module

You may want to create a function that is available to all forms in your program. You can put it in a module and make it public so that it may be used by any form.

MODULE CODE:

 Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
   Dim KeyHand&
   Dim datatype&
   Call RegOpenKey(HKEY, sPath, KeyHand&)
   GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
   Call RegCloseKey(KeyHand&)
 End Function

FORM CODE:

 strRetval = MODULE_NAME.GetSettingEx(HKEY_CURRENT_USER, "Software\album", "DefaultStorybookPath")