Hi, I am new to programming and I have designed a microsoft access form that require users to enter their details which is then kept in a table. I am trying to create a list view form such that all the records can be viewed from their. I have tried to adapt a copy of the list view code that i found on this site for the purpose but I keep getting an error "Invalid use of Me Keyword" on Line 12 of my code (see copy of the code below). I have read that Me keyword can not be used in a module. what can I use to replace the Me Keyword in the code? Are there other ways of creating a List view form that has it's data populated from a table? Do you have links to websites or books with information about how to create list view form with the information populated from a table? Any help will be well appreciated. thank you.
Public Sub FillCDOrder()
On Error GoTo ErrorHandler
'Set Reference to Microsoft DAO 3.xx Library.
'set variables
Dim rs As DAO.Recordset
Dim db As Database
Dim lstItem As ListItem
Dim strSQL As String
Set db = CurrentDb()
strSQL = "SELECT * FROM CDOrder"
Set rs = db.OpenRecordset(strSQL)
With Me.CDOrder & RegistrationListView
'Set ListView style
.View = lvwReport
'This is not supported by ListView 5
.GridLines = True
.FullRowSelect = True
'Clear Header and ListItems
.ListItems.Clear
.ColumnHeaders.Clear
End With
'Set up column headers
With CDOrder & RegistrationListView.ListView1.ColumnHeaders
.Add , , "Identification No", 1000, lvwColumnLeft
.Add , , "First Name", 2000, lvwColumnLeft
.Add , , "Last Name", 2000, lvwColumnLeft
.Add , , "1stLineOfAddress", 1500, lvwColumnRight
.Add , , "2ndLineOfAddress", 1500, lvwColumnRight
.Add , , "City", 700, lvwColumnLeft
.Add , , "PostCode", 700, lvwColumnLeft
.Add , , "PhoneNumber", 1500, lvwColumnRight
.Add , , "EmailAddress", 1500, lvwColumnRight
.Add , , "CDRequest", 1500, lvwColumnRight
.Add , , "DateEnrolled", 1500, lvwColumnRight
.Add , , "TypeOfDelivery", 1500, lvwColumnRight
.Add , , "Donation", 1500, lvwColumnRight
.Add , , "AmountDonated", 1500, lvwColumnRight
End With
' Add items and subitems to list control.
rs.MoveFirst
Do Until rs.EOF
Set lstItem = CDOrder & RegistrationListView.ListView1.ListItems.Add()
lstItem.Text = rs!IdentificationNo
lstItem.SubItems(1) = Nz(Trim(rs!FirstName))
lstItem.SubItems(2) = Nz(Trim(rs!LastName))
lstItem.SubItems(3) = Nz(Trim(rs!FirstLineOfAddress))
lstItem.SubItems(4) = Nz(Trim(rs!SecondLineOfAddress))
lstItem.SubItems(5) = Nz(Trim(rs!City))
lstItem.SubItems(6) = Nz(Trim(rs!PostCode))
lstItem.SubItems(7) = Nz(Trim(rs!PhoneNumber))
lstItem.SubItems(8) = Nz(Trim(rs!EmailAddress))
lstItem.SubItems(9) = Nz(Trim(rs!CDRequest))
lstItem.SubItems(10) = Nz(Trim(rs!DateEnrolled))
lstItem.SubItems(11) = Nz(Trim(rs!TypeOfDelivery))
lstItem.SubItems(12) = Nz(Trim(rs!Donation))
lstItem.SubItems(13) = Nz(Trim(rs!AmountDonated))
'formatting date
lstItem.SubItems(10) = Format(rs!DateEnrolled, "Medium Date")
lstItem.SubItems(10) = Format(rs!DateEnrolled, "dd-mmm-YYYY")
'formatting currency
lstItem.SubItems(13) = Format(rs!AmountDonated, "£#,##,0.00#")
'Next row
rs.MoveNext
Loop
'close recordset
rs.Close
DoCmd.Echo True
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 3021 Then ' no current record
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Sub
Public Sub FillCDOrder()
On Error GoTo ErrorHandler
'Set Reference to Microsoft DAO 3.xx Library.
'set variables
Dim rs As DAO.Recordset
Dim db As Database
Dim lstItem As ListItem
Dim strSQL As String
Set db = CurrentDb()
strSQL = "SELECT * FROM CDOrder"
Set rs = db.OpenRecordset(strSQL)
With Me.CDOrder & RegistrationListView
'Set ListView style
.View = lvwReport
'This is not supported by ListView 5
.GridLines = True
.FullRowSelect = True
'Clear Header and ListItems
.ListItems.Clear
.ColumnHeaders.Clear
End With
'Set up column headers
With CDOrder & RegistrationListView.ListView1.ColumnHeaders
.Add , , "Identification No", 1000, lvwColumnLeft
.Add , , "First Name", 2000, lvwColumnLeft
.Add , , "Last Name", 2000, lvwColumnLeft
.Add , , "1stLineOfAddress", 1500, lvwColumnRight
.Add , , "2ndLineOfAddress", 1500, lvwColumnRight
.Add , , "City", 700, lvwColumnLeft
.Add , , "PostCode", 700, lvwColumnLeft
.Add , , "PhoneNumber", 1500, lvwColumnRight
.Add , , "EmailAddress", 1500, lvwColumnRight
.Add , , "CDRequest", 1500, lvwColumnRight
.Add , , "DateEnrolled", 1500, lvwColumnRight
.Add , , "TypeOfDelivery", 1500, lvwColumnRight
.Add , , "Donation", 1500, lvwColumnRight
.Add , , "AmountDonated", 1500, lvwColumnRight
End With
' Add items and subitems to list control.
rs.MoveFirst
Do Until rs.EOF
Set lstItem = CDOrder & RegistrationListView.ListView1.ListItems.Add()
lstItem.Text = rs!IdentificationNo
lstItem.SubItems(1) = Nz(Trim(rs!FirstName))
lstItem.SubItems(2) = Nz(Trim(rs!LastName))
lstItem.SubItems(3) = Nz(Trim(rs!FirstLineOfAddress))
lstItem.SubItems(4) = Nz(Trim(rs!SecondLineOfAddress))
lstItem.SubItems(5) = Nz(Trim(rs!City))
lstItem.SubItems(6) = Nz(Trim(rs!PostCode))
lstItem.SubItems(7) = Nz(Trim(rs!PhoneNumber))
lstItem.SubItems(8) = Nz(Trim(rs!EmailAddress))
lstItem.SubItems(9) = Nz(Trim(rs!CDRequest))
lstItem.SubItems(10) = Nz(Trim(rs!DateEnrolled))
lstItem.SubItems(11) = Nz(Trim(rs!TypeOfDelivery))
lstItem.SubItems(12) = Nz(Trim(rs!Donation))
lstItem.SubItems(13) = Nz(Trim(rs!AmountDonated))
'formatting date
lstItem.SubItems(10) = Format(rs!DateEnrolled, "Medium Date")
lstItem.SubItems(10) = Format(rs!DateEnrolled, "dd-mmm-YYYY")
'formatting currency
lstItem.SubItems(13) = Format(rs!AmountDonated, "£#,##,0.00#")
'Next row
rs.MoveNext
Loop
'close recordset
rs.Close
DoCmd.Echo True
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 3021 Then ' no current record
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Sub