Creating Public and Private Functions in VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

Standard Private 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

example

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")

troubleshooting

  • Compile Error: Method or data member not found

I bet you made the function "Private" instead of "Public". Make sure the function in the module is a "Public Function".