To rename a class:-
1) open the VBA IDE
(Alt-F11 on the keyboard)
2) Click on the class in Project Explorer
(Ctrl-R on the keyboard if Project Explorer isn't open)
3) Double Click on the class name in the properties window
(F4 on the keyboard if Properties isn't open)
Having said that you really should decide on a class name when you first
create it and stick with that.
The difference between a Module an a Class module.
A module holds code which is global to the project. The code in a module is
frequently a number of functions which are relatively unrelated.
A class module is a code template which represents something. The code in a
class module is all related and should be encapsulated (i.e. relies only on
itself) although this is not by any stretch of the imagination always
possible.
To show the differences have a look at
http://www.mvps.org/access/api/api0001.htm which shows how to use the
Open/Save File dialog in a standard module.
Personally I prefer to use a class module for this as it matches more easily
with the common dialog control and allows me to more easily convert apps
where that control has ben used, also thinking of the Open/Save dialog as a
form or object is more intuitive to me. So the code for a class module
tohandle thiscoule look like this.
' ******* Code Start ********
Option Explicit
'Code Courtesy of
'Terry Kreft
Private Type OPENFILENAME
lStructSize As Long
hWnd As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName Lib _
"comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib _
"comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As OPENFILENAME) As Long
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1
Private lngHwnd As Long
Private wMode As Integer
Private szDialogTitle As String
Private szFileName As String
Private szFilter As String
Private szDefDir As String
Private szDefExt As String
Private szFileTitle As String
Private szFileDir As String
Private intFilterIndex As Integer
Public Function Action() As String
Dim x As Long, OFN As OPENFILENAME
Call SetDefs
With OFN
.lStructSize = Len(OFN)
.hWnd = lngHwnd
.lpstrTitle = szDialogTitle
.lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
.nMaxFile = 255
.lpstrFileTitle = String$(255, 0)
.nMaxFileTitle = 255
.lpstrFilter = szFilter
.nFilterIndex = intFilterIndex
.lpstrInitialDir = szDefDir
.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
x = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or
OFN_PATHMUSTEXIST
x = GetSaveFileName(OFN)
End If
If x <0 Then
If InStr(.lpstrFile, Chr$(0)) 0 Then
szFileName = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
szFileTitle = Left$(.lpstrFileTitle, InStr(.lpstrFileTitle,
Chr$(0)) - 1)
Call getFile_Dir
End If
Else
szFileName = ""
End If
End With
Action = szFileName
End Function
'Pass a bar separated string and returns a Null separated string
Private Function NullSepString(ByVal BarString As String) As String
Dim intInstr As Integer
Const vbBar = "|"
Do
intInstr = InStr(BarString, vbBar)
If intInstr 0 Then Mid(BarString, intInstr, 1) = vbNullChar
Loop While intInstr 0
NullSepString = BarString
End Function
Private Sub getFile_Dir()
Dim intInstr As Integer
intInstr = InStr(szFileName, szFileTitle) - 1
szFileDir = Left(szFileName, intInstr)
End Sub
Property Let hWnd(SourceHwnd As Long)
lngHwnd = SourceHwnd
End Property
Property Let ModeOpen(DialogMode As Boolean)
wMode = DialogMode * -1
End Property
Property Let Title(DialogTitle As String)
szDialogTitle = DialogTitle
End Property
Property Let FileName(DefaultFile As String)
szFileName = DefaultFile
End Property
Property Get FileName() As String
FileName = szFileName
End Property
Property Let Filter(FilterList As String)
szFilter = NullSepString(FilterList)
End Property
Property Let StartDir(InitialDir As String)
szDefDir = InitialDir
End Property
Property Let DefaultExtension(DefExt As String)
szDefExt = DefExt
End Property
Property Get FileTitle()
FileTitle = szFileTitle
End Property
Property Get FileDir() As String
FileDir = szFileDir
End Property
Private Sub SetDefs()
If lngHwnd = 0 Then lngHwnd = 0
If szFilter = "" Then szFilter = NullSepString("All Files|*.*")
If szDefDir = "" Then szDefDir = "C:\"
If intFilterIndex = 0 Then intFilterIndex = 1
End Sub
' ******* Code End ********
If we called this class cDialog then the code to use it could be as follows
' ******* Code Start ********
Dim cD As cDialog
Dim strFileName As String
Set cD = New cDialog
With cD
.Filter = "Excel Files|*.xls|All Files|*.*"
.StartDir = "c:\"
.Title = "Find Excel File ..."
.ModeOpen = True
strFileName = .Action
End With
Set cD = Nothing
Debug.Print strFileName
' ******* Code End ********
--
Terry Kreft
"JonathanOrlev" <Jo***********@gmail.comwrote in message
news:11**********************@i15g2000cwa.googlegr oups.com...
Hello everyone,
I have a newbe question:
In Access (2003) VBA, what is the difference between a Module and a
Class Module in the VBA development environment?
If I remember correctly, new types of objects (classes) can only be
defined in Class modules.
Am I correct?
Also, can you please tell me what is the keyword used to define a new
class?
If I have the keyword, I can probably be able to find the help in MS
Access's help program, but without it I cannot find it.
apropos the help system,
I think that Office 2003 help system is horrible, probably the worst I
ever seen. I almost cannot find anything I need, including things I
know their name. I just don't know how this help system works. For
example, this system does not recognize quotes: If I type "A B", it
considers it as A and B, and maybe A or B, I don't know.
I don't need explanations about it, but please tell me if you also it
is so horrible.
Thanks,
Jonathan Orlev