Difference between revisions of "Hints and Recipes for VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m
 
(One intermediate revision by one user not shown)
Line 14: Line 14:
 
   End With
 
   End With
 
   cboDelimiter = "TAB"
 
   cboDelimiter = "TAB"
 
== Write/Create a new file using FileSystemObject ==
 
 
*must add 'reference' to "Microsoft Scripting Runtime"
 
 
  Dim fso As New FileSystemObject, fsoStream As TextStream
 
  Dim rec1, strLine, strFileSpec
 
  Dim cnt0 As Integer, tag1 As Integer
 
  strFileSpec = Environ("HOMEPATH") & "\My Documents\kaching.log"
 
  strLine = "kaching incident " & Date & " " & Format(time, "HH:mm:ss") & vbCrLf
 
  Set fsoStream = fso.CreateTextFile(strFileSpec, True)
 
  fsoStream.Write strLine
 
  fsoStream.Close
 
  Set fsoStream = Nothing: Set fso = Nothing
 
 
== Write if exists, create if doesn't exist using FileSystemObject ==
 
 
  If fso.FileExists(strFileSpec) Then
 
    Set fsoStream = fso.OpenTextFile(strFileSpec, 2)
 
  Else
 
    Set fsoStream = fso.CreateTextFile(strFileSpec, True)
 
  End If
 
 
 
  fsoStream.Write strLine
 
  fsoStream.Close
 
  Set fsoStream = Nothing: Set fso = Nothing
 
 
  ' strFileSpec, 1 : ForReading 2 : ForWriting  8 : ForAppending 
 
 
== Write to control on another form and display ==
 
 
  Dim lblCaught As Label
 
  Set lblCaught = frmKaching.lblCaught
 
  frmKaching.WindowState = vbNormal
 
  lblCaught = "You've Been Naughty!"
 
  frmKaching.Show
 
  
 
== Delay without using a Timer control ==
 
== Delay without using a Timer control ==
Line 60: Line 24:
 
     Loop
 
     Loop
 
   End Sub
 
   End Sub
 
== Determine is a form has been loaded ==
 
 
The wrong way:
 
  if form1.visible = true then
 
The reason:
 
 
Due to the way an intrinsic variable instance works in VB, the line above will actually cause form1 to load.  Forms are loaded automatically whenever referenced in code, whether loaded visibly or not.
 
 
Special thanks to a gentleman named Steve Gerrard who posted a correct sample script on another forum.  His example works by going through a list of loaded forms and matching by the form name you are looking for.
 
 
  Public Function FormIsLoaded(FormName As String) As Boolean
 
    Dim oFrm As Form
 
    For Each oFrm In Forms
 
      If oFrm.Name = FormName Then
 
        FormIsLoaded = True
 
        Exit For
 
      End If
 
    Next oFrm
 
  End Function
 
 
lovely!
 
  
 
== Quickly determine which option button is selected ==
 
== Quickly determine which option button is selected ==
Line 94: Line 36:
  
 
Credit to [http://msdnx.com/visual-basic-archive/23/43-36-236214.shtm Ed Lampman for this VB trick].
 
Credit to [http://msdnx.com/visual-basic-archive/23/43-36-236214.shtm Ed Lampman for this VB trick].
 +
 +
== Command Line Parameter ==
 +
 +
This example was taking from the Sales Logix Assistant Program:
 +
Private Function CheckLauncherParamter()
 +
' blnDebug
 +
  If InStr(LCase(Command), "/verified") > 0 Then
 +
    CheckLauncherParamter = True
 +
    blnDebug = False
 +
    mnuFileExit.Enabled = False
 +
  ElseIf InStr(LCase(Command), "/debug") > 0 Then
 +
    CheckLauncherParamter = True
 +
    blnDebug = True
 +
    mnuFileExit.Enabled = True
 +
  Else
 +
    CheckLauncherParamter = False
 +
  End If
 +
End Function
 +
  
 
 
 
 

Latest revision as of 15:24, 12 February 2008

Date and Time examples with formatting options

 strTimeStamp = Date & " " & Format(Time, "HH:mm:ss")
 strMsg = strMsg & "date/time of request: " & Date & " " & Time & vbCrLf
 strSQLdate = Format(Date, "M/d/yyyy")

Combobox

 With cboDelimiter
   .AddItem "TAB"
   .AddItem "PIPE"
   .AddItem "SPACE"
   .AddItem "fancy!"
 End With
 cboDelimiter = "TAB"

Delay without using a Timer control

 Public Sub LongDelay(intSeconds As Long)
   Dim cnt As Double
   cnt = Timer + intSeconds
   Do While Timer < cnt
     DoEvents
   Loop
 End Sub

Quickly determine which option button is selected

Often OptionButton controls are arranged in control arrays. To quickly find the index of the only selected OptionButton control apply this little math trick. The following example assumes there are 3 option buttons in a control array.

intSelected = Option(0).Value * 0 - Option(1).Value * 1 - Option(2).Value * 2

Note that the first operand is always zero, and you can simplify the above expression as follows:

intSelected = -Option(1).Value - Option(2).Value * 2

Credit to Ed Lampman for this VB trick.

Command Line Parameter

This example was taking from the Sales Logix Assistant Program:

Private Function CheckLauncherParamter()
' blnDebug
  If InStr(LCase(Command), "/verified") > 0 Then
    CheckLauncherParamter = True
    blnDebug = False
    mnuFileExit.Enabled = False
  ElseIf InStr(LCase(Command), "/debug") > 0 Then
    CheckLauncherParamter = True
    blnDebug = True
    mnuFileExit.Enabled = True
  Else
    CheckLauncherParamter = False
  End If
End Function