Click here to Skip to main content
15,891,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have created a database to be a data entry tool for several users to enter information about an event. In two places in the datbase form the user will need to have the ability to add multiple records pertaining to one entity. The user should be able to add, edit, and delete these records and then continue in the form.

I am utilizing three forms.
1. A main form called [Claims Review]with listbox, add button, delete button
2. frmAddGuideline with bound record set to dataentry, add button, cancel button
3. frmEditGuideline with bound record not set to dataentry, update button, cancel button

Record source for form 2 and 3 is a query off table called tblGuideline. tblGuideline has 5 fields:
GuidelineID
ClaimID
Guideline
Entity
year

I can add a guideline perfectly using this code:
main form
VB
Private Sub btnAddGuideline_Click()
DoCmd.OpenForm "frmAddGuideline", acNormal
End Sub


frmAddGuideline
VB
Private Sub btnAddGuideline_Click()
DoCmd.Close acForm, "frmAddGuideline"
'refresh listbox from subform
Forms![Claim Review]!lstbxGuideline.Requery
End Sub

Private Sub btnCancelGuideline_Click()
'Delete changes to record
If Me.Dirty Then Me.Undo
'close form
DoCmd.Close acForm, "frmAddGuideline"
End Sub

Private Sub Form_Load()
Me.ClaimID = [Forms]![Claim Review]![ClaimID]
End Sub


Can't delete record!

VB
Private Sub btnEditGuideline_Click()
'Nothing Selected
If lstbxGuideline.ListIndex > -1 Then

'Item Selected for update/delete
strGuideline = lstbxGuideline.ListIndex(Value)

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strGuideline As String
Dim strSQL As String

Set db = CurrentDb()

strSQL = "SELECT * FROM tblGuideline "
strSQL = strSQL & "WHERE tblGuideline.GuidelineID =" & strGuideline & " "

db.QueryDefs.Delete ("qryEditGuidelinesItems")
Set qdf = db.CreateQueryDef("qryEditGuidelinesItems", strSQL)

DoCmd.OpenForm "frmEditGuideline", acNormal

Else
MsgBox "Error", "Select an Item in the listbox to Edit", vbOKOnly
Set db = Nothing
Set qdf = Nothing

End If
End Sub


frmEditGuideline
Private Sub btnCancelEditGuideline_Click()
'Delete changes to record
If Me.Dirty Then Me.Undo
'close form
DoCmd.Close acForm, "frmEditGuideline"
End Sub

Private Sub btnUpdateGuideline_Click()
Dim db As DAO.Database
Set db = CurrentDb
If Me.CheckDeleteGuideline = True Then
'Delete Record
Dim strSQL As String
Dim strGuideline As Variant
strGuideline = [Forms]![frmEditGuideline]![GuidelineID]
strSQL = "DELETE * FROM [tblGuideline] WHERE (tblGuideline.GuidelineID = " & strGuideline & ")"
db.Execute (strSQL), dbFailOnError
End If
'Close form, save record in tbl
DoCmd.Close acForm, "frmEditGuideline"
'refresh listbox from subform
Forms![Claim Review]!lstbxGuideline.Requery
End Sub

Private Sub Form_Load()
Me.ClaimID = [Forms]![Claim Review]![ClaimID]
End Sub



My problem is the listindex is not the value of the bound control.. please help!

Thanks,
Christina
Posted
Updated 5-Apr-11 9:56am
v2

Hi,

Years since I last used Access, but have you tried using:
strGuideline = lstbxGuideline.ItemData(lstbxGuideline.ListIndex)

If the list box is a multiselect version then you would have to loop through all items and check if the Selected property is true.
 
Share this answer
 
Comments
ChristinaVelez 5-Apr-11 16:17pm    
Tried something similar I think.. I've been playing around with it too much. Tried your code and it gave me an error too... err!

Thanks for your suggestion, much appreciated!
Wendelius 5-Apr-11 16:23pm    
What kind of error did you get?
ChristinaVelez 5-Apr-11 16:49pm    
Actually it was a compile err. I had to define my variable above where I was giving it value!

I got it to work now!!
strGuideline = lstbxGuideline.ItemData(lstbxGuideline.ListIndex + 1) -- The +1 since the listIndex begins at 0

My form recordsource was not using the qry which is set for the qdef "qryEditGuidelinesItems"

THANK YOU SOOO MUCH! Amazing what one little piece of code feedback can do!
Wendelius 5-Apr-11 16:55pm    
Glad you got it resolved :)
strGuideline = lstbxGuideline.ItemData(lstbxGuideline.ListIndex + 1)
form recordsource set to qdef "qryEditGuidelinesItems"

whew!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900