Hi all
This post is an extension of the previous post (how to develop a carousel).
Here I will show how to manage a summary form linked to a detail one, in order to show data and detail them.
I want to clarify that this is not "the way" to work with access, this is "a way". This is the way I use. After been developing Access applications, I've found this is a good solution, and I use it almost always.
I will improve the previous example, the used cars seller adding a new form.
The new form has a listbox and some buttons (add, delete, duplicate and modify) to perform those operations. All those operations are solved using VBA, as you may see.
The new form is named Main. You may see in the next screenshot I've added new records.
Once the form is loaded, the data are stored in the listbox using the Form_load event.
The objects are named as follows:
Let's take a minute to analyze the souce code on the form (remember, if you want to do this example step by step, you have to indicate Access you want an event procedure for each object, before to write the code. If you just write the code by copy-paste it will not work, because Access does not knows it):
Event Load Form
In this event, before to load the form, the code prepare the form.
Private Sub Form_Open(Cancel As Integer)
Me.lstData.RowSource = "SELECT idVehicle, PlateNumber, Chassis, DateFirstReg, Brand, Model, Version FROM Vehicles ORDER BY idVehicle"
End Sub
With this sentence we may fill the listbox each time we open the form.
I've adjusted those properties for this listbox in the Format section:
Button add: cmdAdd (event cmdAdd click)
This button opens the detail form to enter a new car in the database.
Private Sub cmdAdd_Click()
DoCmd.OpenForm "Vehicles", acNormal,,, acFormAdd, acDialog
Me.lstData.Requery
End Sub
Easy: opens the form Vehicles, only to add records, and as a Dialog, that is, it puts the window over the other windows, and you can't access the other windows until you close Vehicles.
After closing this window, it refreshes lstData to see the new vehicle added, by re-reading the data table.
Button delete: cmdDelete (event cmdDelete click)
This button deletes the car from the database. There's no form to show, only a message box to ensure the user whishes to delete the record. It only works if there's something selected in lstData.
Private Sub cmdDelete_Click()
Dim sqlAction As String, answer As Long, Message As String
If Not IsNull(Me.lstData) Then
Message = "This operation cannot be undone." & vbCr & "Are you sure you wish to continue?"
answer = MsgBox(Message, vbCritical + vbYesNo, "Delete vehicle")
If answer = vbYes Then
sqlAction = "DELETE * FROM Vehicles WHERE idVehicle = " & Me.OpenArgs
CurrentDb.Execute sqlAction
End If
Me.lstData.Requery
End If
End Sub
Button duplicate: cmdDup (event cmdDup click)
This button duplicates a car, without showing any form. Simply duplicate and refresh the listbox on Summary.
Private Sub cmdDuplicate_Click()
Dim sqlAction As String, recData As Recordset
If Not IsNull(Me.lstData) Then
sqlAction = "SELECT PlateNumber, Chassis, DateFirstReg, Brand, Model, Version " & _
"FROM Vehicles " & _
"WHERE idVehicle = " & Me.-OpenArgs
Set recData = CurrentDb.OpenRecordset(sqlAction)
sqlAction = "INSERT INTO Vehicles (PlateNumber, Chassis, DateFirstReg, Brand, Model, Version) " & _
"VALUES ('" & recData.Fields("PlateNumber") & "', " & _
"'" & recData.Fields("Chassis") & "', " & _
"#" & Format(recData.Fields("DateFirstReg"), "mm/dd/yyyy") & "#, " & _
"'" & recData.Fields("Brand") & "', " & _
"'" & recData.Fields("Model") & "', " & _
"'" & recData.Fields("Version") & "'" & _
")"
CurrentDb.Execute sqlAction
recData.Close
Me.lstData.Requery
End If
End Sub
Button modify: cmdMod (event cmdMod click)
This button opens the detail form to edit the car.
Private Sub cmdModify_Click()
DoCmd.OpenForm "Vehicles", acNormal, , "idVehicle = " & Me.lstData, acFormEdit, acDialog
Me.lstData.Requery
End Sub
Button close: cmdClose (event cmdClose click)
This button closes the form when is pressed.
Private Sub cmdClose_Click()
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
This post is an extension of the previous post (how to develop a carousel).
Here I will show how to manage a summary form linked to a detail one, in order to show data and detail them.
I want to clarify that this is not "the way" to work with access, this is "a way". This is the way I use. After been developing Access applications, I've found this is a good solution, and I use it almost always.
I will improve the previous example, the used cars seller adding a new form.
The new form has a listbox and some buttons (add, delete, duplicate and modify) to perform those operations. All those operations are solved using VBA, as you may see.
The new form is named Main. You may see in the next screenshot I've added new records.
Once the form is loaded, the data are stored in the listbox using the Form_load event.
The objects are named as follows:
- Button Add is named cmdAdd
- Button Duplicate is named cmdDuplicate
- Button Delete is named cmdDelete
- Button Modify is named cmdModify
- Button Close is named cmdClose
- List box is named lstData
Let's take a minute to analyze the souce code on the form (remember, if you want to do this example step by step, you have to indicate Access you want an event procedure for each object, before to write the code. If you just write the code by copy-paste it will not work, because Access does not knows it):
Event Load Form
In this event, before to load the form, the code prepare the form.
Private Sub Form_Open(Cancel As Integer)
Me.lstData.RowSource = "SELECT idVehicle, PlateNumber, Chassis, DateFirstReg, Brand, Model, Version FROM Vehicles ORDER BY idVehicle"
End Sub
With this sentence we may fill the listbox each time we open the form.
I've adjusted those properties for this listbox in the Format section:
- Number of columns: 7. There are 7 fields in the selection query.
- Columns height: 0 cm (I think in US will be 0 in). Like this, the first field, idVehicle, is hidden. Clearly is a serial number automatically generated, so we don't need to see it because we identify the vehicles by their plate.
- Column Headings: Yes, like this we may see the field names. It is not necessary, but is useful.
Button add: cmdAdd (event cmdAdd click)
This button opens the detail form to enter a new car in the database.
Private Sub cmdAdd_Click()
DoCmd.OpenForm "Vehicles", acNormal,,, acFormAdd, acDialog
Me.lstData.Requery
End Sub
Easy: opens the form Vehicles, only to add records, and as a Dialog, that is, it puts the window over the other windows, and you can't access the other windows until you close Vehicles.
After closing this window, it refreshes lstData to see the new vehicle added, by re-reading the data table.
Button delete: cmdDelete (event cmdDelete click)
This button deletes the car from the database. There's no form to show, only a message box to ensure the user whishes to delete the record. It only works if there's something selected in lstData.
Private Sub cmdDelete_Click()
Dim sqlAction As String, answer As Long, Message As String
If Not IsNull(Me.lstData) Then
Message = "This operation cannot be undone." & vbCr & "Are you sure you wish to continue?"
answer = MsgBox(Message, vbCritical + vbYesNo, "Delete vehicle")
If answer = vbYes Then
sqlAction = "DELETE * FROM Vehicles WHERE idVehicle = " & Me.OpenArgs
CurrentDb.Execute sqlAction
End If
Me.lstData.Requery
End If
End Sub
Button duplicate: cmdDup (event cmdDup click)
This button duplicates a car, without showing any form. Simply duplicate and refresh the listbox on Summary.
Private Sub cmdDuplicate_Click()
Dim sqlAction As String, recData As Recordset
If Not IsNull(Me.lstData) Then
sqlAction = "SELECT PlateNumber, Chassis, DateFirstReg, Brand, Model, Version " & _
"FROM Vehicles " & _
"WHERE idVehicle = " & Me.-OpenArgs
Set recData = CurrentDb.OpenRecordset(sqlAction)
sqlAction = "INSERT INTO Vehicles (PlateNumber, Chassis, DateFirstReg, Brand, Model, Version) " & _
"VALUES ('" & recData.Fields("PlateNumber") & "', " & _
"'" & recData.Fields("Chassis") & "', " & _
"#" & Format(recData.Fields("DateFirstReg"), "mm/dd/yyyy") & "#, " & _
"'" & recData.Fields("Brand") & "', " & _
"'" & recData.Fields("Model") & "', " & _
"'" & recData.Fields("Version") & "'" & _
")"
CurrentDb.Execute sqlAction
recData.Close
Me.lstData.Requery
End If
End Sub
Button modify: cmdMod (event cmdMod click)
This button opens the detail form to edit the car.
Private Sub cmdModify_Click()
DoCmd.OpenForm "Vehicles", acNormal, , "idVehicle = " & Me.lstData, acFormEdit, acDialog
Me.lstData.Requery
End Sub
Button close: cmdClose (event cmdClose click)
This button closes the form when is pressed.
Private Sub cmdClose_Click()
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
Well, the rest of the example works the same than the previous week.
It's short and very simple, but will help in the next week. I will improve the example using system commands to import the pictures.
Enjoy it. You may download here.
Have a nice week.
Comentaris
Publica un comentari a l'entrada