| General |
| Variable Declaration Shortcuts |
| Dim c@ = Dim c As Currency |
| Dim d# = Dim d As Double |
| Dim i% = Dim i As Integer | 32,767 |
| Dim l& = Dim l As Long | 2,147,483,647 |
| Dim n! = Dim n As Single |
| Dim s$ = Dim s As String |
| Dim v = Dim v As Variant |
|
| Strings, Text & Dates |
Add a Slash to DirName
Assume: dName$ = "C:\mydir"
Called: dName = AddSlash(dName) - or
dName = AddSlash(dName, "/")
Private Function AddSlash$(ByVal f$, _
Optional t$ = "\")
If Right$(f, 1) <> t Then f = f & t
AddSlash = f
End Function
|
| Files, Folders & Directories |
Check if File Exists
Assume: fName$ = "C:\mydir\myfile.txt"
Called: If FileExists(fName) Then
Private Function FileExists(fName$) As Boolean
FileExists = (Dir$(sName) <> "")
End Function
|
Check if Directory Exists
Called: If DirExists(dName) Then
Private Function DirExists(dName$) As Boolean
DirExists = (Dir$(dName, vbDirectory) <> "")
End Function
|
Load a text file into a Textbox
'Sequential file access
Dim f As Integer
f = Freefile
Open fName For Input As f
Text1.Text = Input(Lof(f), f)
Close f
Load a text file in a RichTextBox
RichTextBox1.LoadFile fPathName, rtfText
|
Open for Random
Private Type recType
DeleteCode As Byte
SortFlag As Byte
CustCode As Long
CustFirstName As String * 16
CustLastName As String * 18
End Type
Dim recData As recType
Dim fHnd As Integer
Dim numRecs As Long
Private Sub Command1_Click()
fHnd = Freefile
numRecs = Len(recData)
Open fName For Random As fHnd Len = numRecs
numRecs = Lof(fHnd) \ numRecs
End Sub
|
| Forms and Controls |
Textbox
Make a textbox scroll to the bottom
Text1.SelStart = Len(Text1.Text)
|
Unload All Forms
Private Sub Form_Unload(Cancel As Integer)
Dim f As Form
For Each f In Forms
If Not f Is Me Then Unload f
Next
End Sub
|
| Data Access Code |
ADO Connection
Dim cnStr As String
Dim cn as New ADODB.Connection
cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\mydir\myDB.mdb;" & _
"UID=username;PWD=password;"
cn.Open cnStr
ADO Recordset
Dim strSQL As String
Dim rs as NewADODB.Recordset
strSQL = "SELECT * FROM mytable"
rs.Open strSQL, cn
|
DAO Connection
Dim db As Database
Set db = OpenDatabase("c:\mydir\myDB.mdb")
DAO Recordset
Dim rs As Recordset
Set rs = db.OpenRecordset(strSQL)
|
| Application Program Interface (API) |
Shell Execute - Run a program
ShellExecuting the following 'index.html'
will run the associated (default) browser
Private Declare Function ShellExecute _
Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Sub Command1_Click()
Dim sFile As String
Dim sCommand As String
Dim sWorkDir As String
'The file to execute
sFile = "C:\mydir\index.html"
'Command line parameters
sCommand = vbNullString
'The working directory
sWorkDir = "C:\mydir"
ShellExecute Me.hWnd, "open", _
sFile, sCommand, sWorkDir, 1
End Sub
|