P: 1
| -
Dim db As Database
-
Dim rs As Recordset
-
Dim WS As Workspace
-
Dim Max As Long
-
-
-
Private Sub Form_Load()
-
Set WS = DBEngine.Workspaces(0)
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
Set rs = db.OpenRecordset("Addresses", dbOpenTable)
-
Max = rs.RecordCount
-
lblTotalContacts.Caption = Max
-
If rs.RecordCount = 0 Then
-
Exit Sub
-
Else
-
rs.MoveFirst
-
-
List1.Clear
-
-
For i = 1 To Max
-
List1.AddItem rs!FullName
-
rs.MoveNext
-
Next i
-
List1.ListIndex = 0
-
End If
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
End
-
End Sub
-
-
Private Sub List1_Click()
-
Set WS = DBEngine.Workspaces(0)
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
-
On Error GoTo ErrorHandler
-
lblName.Caption = rs("FullName")
-
lblNationality.Caption = rs("Nationality")
-
lblCountry.Caption = rs("Country")
-
lblBirthday.Caption = rs("Birthday")
-
lblCountry.Caption = rs("Country")
-
lblCity.Caption = rs("City")
-
lblAddress.Caption = rs("Address")
-
lblPhoneNumber.Caption = rs("PhoneNumber")
-
lblMobile.Caption = rs("Mobile")
-
lblFax.Caption = rs("Fax")
-
lblEMAIL.Caption = rs("E-Mail")
-
lblWebSite.Caption = rs("WebSite")
-
lblCompany.Caption = rs("Company")
-
Text1.Text = rs("LittleComment")
-
ErrorHandler:
-
Resume Next
-
End Sub
| |
Share this Question
Expert 100+
P: 305
| -
'declare variables to be used and in this case these variables represent DAO (Data Access Object) objects although so you can understand more the order has been changed by me to more represent the heirarchy of the objects
-
Dim WS As Workspace
-
Dim db As Database
-
Dim rs As Recordset
-
-
Dim Max As Long
-
-
-
'this even is called after initialize
-
Private Sub Form_Load()
-
-
'create the workspace via the dbengine (jet) object
-
Set WS = DBEngine.Workspaces(0)
-
-
'this example you have shown is an example of bad programming practice as none of these variables are declared. To remedy this place Option Explicit at top of code window prior to any declarations
-
DbFile = (App.Path & "\Database\AddressBook.mdb")
-
PwdString = "swordfish"
-
-
'open the database
-
Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
-
'open a recordset, namely the addresses table
-
Set rs = db.OpenRecordset("Addresses", dbOpenTable)
-
-
'retrieve the number of records and display
-
Max = rs.RecordCount
-
lblTotalContacts.Caption = Max
-
-
'check to see if there are any records
-
If rs.RecordCount = 0 Then
-
-
'no records so exit this sub
-
Exit Sub
-
Else
-
-
'have records so position the recordset cursor to the first record
-
rs.MoveFirst
-
-
'clear list1
-
List1.Clear
-
-
'now run through rs and add the information to list1
-
For i = 1 To Max
-
List1.AddItem rs!FullName
-
rs.MoveNext
-
Next i
-
List1.ListIndex = 0
-
End If
-
-
'call a sub that enables/disables buttons depending upon...?
-
ButtonsEnabled
-
End Sub
-
-
'this event is called when the mouse moves over the form
-
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
-
'call sub...
-
ButtonsEnabled
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
-
'another example of bad programming practice with vb6 as this does not allow you to properly close your object that you have opened and may lead to memory leaks and system instability
-
End
-
End Sub
-
-
'when user clicks on an item in list1 this event is called
-
Private Sub List1_Click()
-
-
'the following lines are not needed as the workspace is already created and the database is already opened...
-
'Set WS = DBEngine.Workspaces(0)
-
'DbFile = (App.Path & "\Database\AddressBook.mdb")
-
'PwdString = "swordfish"
-
'Set db = DBEngine.OpenDatabase(DbFile, False, False, ";PWD=" & PwdString)
-
-
'open a recordset based upon user selection
-
Set rs = db.OpenRecordset("Select * from Addresses where FullName = '" & Trim(List1.List(List1.ListIndex)) & "'")
-
-
'tell compiler that we will attempt to handle any errors after this label below
-
On Error GoTo ErrorHandler
-
-
'another example of bad programming practice as these should be rs.fields("fieldname") for complete readability and for easier maintence by the noobie
-
lblName.Caption = rs("FullName")
-
lblNationality.Caption = rs("Nationality")
-
lblCountry.Caption = rs("Country")
-
lblBirthday.Caption = rs("Birthday")
-
lblCountry.Caption = rs("Country")
-
lblCity.Caption = rs("City")
-
lblAddress.Caption = rs("Address")
-
lblPhoneNumber.Caption = rs("PhoneNumber")
-
lblMobile.Caption = rs("Mobile")
-
lblFax.Caption = rs("Fax")
-
lblEMAIL.Caption = rs("E-Mail")
-
lblWebSite.Caption = rs("WebSite")
-
lblCompany.Caption = rs("Company")
-
Text1.Text = rs("LittleComment")
-
-
'label for the error handler. Also, missing the exit sub statement before this line
-
ErrorHandler:
-
-
'basically we are telling the compiler that we are trying to ignore any errors and to go to the next line and if that is so then we could have used the On Error Resume Next statement instead of telling the system that we will try to handle the error
-
Resume Next
-
End Sub
Hope that helps
Good Luck
| | | | Question stats - viewed: 2455
- replies: 1
- date asked: Oct 19 '09
| |