Viewing combobox values from textbox values Microsoft Access 2007 - ms-access

I have a form ("Patient Complications") where users input data to a form using 2 cascading combo boxes ("catcombo" and "speccombo"). The combo boxes pull their values from a table ("Complications"). The table has a field for complication category (for example, infection, bleeding, mechanical). A second field lists specific complications (for example, if the complication category is "bleeding", the specific complication could be "GI" or "other"). The input from the combo boxes are concatenated and put into a text field on the form ("Complication"). That part works fine.
My form has several command buttons, including "edit" and "save" command buttons. Since I don't want users interacting with the "complication" field on the form, I have the field become not visible when the "edit" button is clicked. Instead, the 2 combo boxes become visible and allow users to input data. When "save" is selected, the reverse occurs. The two combo boxes become not visible, and the complication field becomes visible and locked.
Unfortunately, when "edit" is selected, the combo boxes are visible but show up blank (nothing is selected or displayed). I am trying to have the boxes display the inputs that were given to the text field. For example, if the text field displays "Bleeding, Other", I want the catcombo box to display "Bleeding" and the speccombo box display "Other". I have been unable to find anything to this effect. If anyone has any ideas it would be greatly appreciated.
The relevant code is included below. Please let me know if I can provide further clarification.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
End Sub
Private Sub speccombo_OnCurrent()
Dim strsql As String
strsql = "SELECT [Complications]![Specific Complication] FROM tblComplications" & _
"WHERE [Complication Category]=Forms![Patient Complications]![catcombo].value"
End Sub
Private Sub speccombo_AfterUpdate()
Forms![Patient Complications]![Complication] = Me.catcombo.Value & ", " & Me.speccombo.Value
End Sub
Private Sub save_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = True
Complication.Visible = True
Complication.Locked = True
comments.Locked = True
catcombo.Visible = False
speccombo.Visible = False
Me.edit.Visible = True
Me.edit.SetFocus
Me.help.Visible = False
Me.save.Visible = False
Me.first.Visible = True
Me.next.Visible = True
Me.previous.Visible = True
Me.last.Visible = True
Me.addnew.Visible = True
Me.close.Visible = True
Me.cancel.Visible = False
End Sub
Private Sub edit_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = False
Complication.Visible = False
comments.Locked = False
catcombo.Visible = True
catcombo.Locked = False
catcombo.Enabled = True
speccombo.Visible = True
speccombo.Locked = False
speccombo.Enabled = True
Me.cancel.Visible = True
Me.cancel.SetFocus
Me.edit.Visible = False
Me.help.Visible = True
Me.save.Visible = True
Me.first.Visible = False
Me.next.Visible = False
Me.previous.Visible = False
Me.last.Visible = False
Me.addnew.Visible = False
Me.close.Visible = False
End Sub

I figured it out. I added a field to the "Complications" table called "Input". This field contains the concatenated values that were put into the patient record (in the example above, the input field would be "Bleeding, Other"). The values in the Input field are the exact values that would be recorded in the "Complication" field on the Patient Complications form. I added the vba code below to the "Edit" command button code.
If Not IsNull(Forms![Patient Complications]![Complication]) Then
Dim comptext As String
Dim spectext As String
comptext = DLookup("[Complication Category]", "Complications", "Input = Forms![Patient Complications]![Complication]")
catcombo.Value = comptext
spectext = DLookup("[Specific Complication]", "Complications", "Input=Forms![Patient Complications]![Complication]")
speccombo.Value = spectext
End If
That solved my initial problem, but I then had an issue with the speccombo box displaying the last value it was given. For example, if speccombo.value="GI" when I click "edit", it would continue to display "GI" until another selection was made. This isn't a huge deal, just inconvenient. I wanted to make the speccombo box essentially zero out if catcombo was changed. I added the code below to fix that problem.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
Me.speccombo.Value = Null
End Sub
Please let me know if I need to provide clarification for anything.

Related

Combo Box Hanging onto old data

I have a form that I have placed a toggle button so the user may clear the data they've previously from three combo boxes. I've created an SQL to delete the data in the table the form is bound to, but the combo boxes are still retaining the data despite the table being clear of any data. How can I reset the combo boxes? Even when I have the form open and close, the old data is still in the combo box. I would like to reset those combos.
Private Sub Reset_Company_Marketer_Product_Click()
Dim DltPrvCompMarkProd_TableSQL As String
DltPrvCompMarkProd_TableSQL = "DELETE User_SubProducts.* FROM User_SubProducts;"
DoCmd.SetWarnings False
DoCmd.RunSQL DltPrvCompMarkProd_TableSQL
DoCmd.SetWarnings True
Private Sub Reset_Company_Marketer_Product_Click()
Dim DltPrvCompMarkProd_TableSQL As String
DltPrvCompMarkProd_TableSQL = "DELETE User_SubProducts.* FROM User_SubProducts;"
Me!Company_Select_Combo = Null
Me!Marketer_Select_Combo = Null
Me!Product_Select_Combo = Null
Company_Select_Combo.Enabled = True
Marketer_Select_Combo.Visible = False
Product_Select_Combo.Visible = False
DoCmd.SetWarnings False
DoCmd.RunSQL DltPrvCompMarkProd_TableSQL
DoCmd.SetWarnings True

Check if unbound text box is equal to a string

In an access form, I have an unbound text box when contains a formula. The formula returns "Yes" or "No" based on whether the form has been completed (it ensure that all required boxes are filled in).
On the same form, I have a command button which I want to use to filter out completed forms. Basically, it is a button that save "Save Record" and when clicked, the form - if completed - should disappear (i.e. be filtered out).
Below is what I put in, but I don't know VBA.
Private Sub bttn_Save_Click()
Me.Filter = Me.MainFormComplete = Yes
Me.FilterOn = True
End Sub
How do I get check to see if my field (MainFormComplete) is equal to "Yes"?
Not quite sure what you are trying to do, but it could be:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete = 'Yes'"
Me.FilterOn = True
End Sub
or:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete <> 'Yes'"
Me.FilterOn = True
End Sub
or
if Me.MainFormComplete = Yes then
....
end if
the textbox might not be readable if text was simply entered and not saved. read the text:
if Me.MainFormComplete.Text = Yes then 'or "Yes" or True
....
end if

Changing main form button visibility from the subform

I've been trying to make a button disappear when a new insert is made and one of the fields is empty, problem is the field is on a subform (data sheet view) and the button on the main form I've been trying the following code with no success.
If Me.Produto = "" Then
[Orçamentos]!Comando33.Visible = False
[Orçamentos]!Comando47.Visible = False
Me.Descrição.Visible = False
End If
Any advice?
Assuming Me is the sub form, and Orçamentos is the main form:
If Me.Produto = "" Then
Me.Parent!Comando33.Visible = False
Me.Parent!Comando47.Visible = False
Me.Descrição.Visible = False
End If

Setting a form's Record Source on Load event

I was hoping to use one form that has 3 layouts, but change the data source as the form loads. Here is the code that is called during OnLoad
Private Sub ShowInputLog(iLogType As Integer)
'Determines the Log Type Layout
'For data entry
Select Case iLogType
Case 1 '"MIPR"
Me.Caption = "MIPR"
Me.RecordSource = "tbl_MIPR"
Me.TabCodes.Pages(0).Visible = True
Me.TabCodes.Pages(0).Enabled = True
Me.cmbPR_NUM.SetFocus
Me.TabCodes.Pages(1).Visible = False
Me.TabCodes.Pages(2).Visible = False
Case 2 '"SPSPR"
Me.RecordSource = "tbl_SPSPR"
Me.Caption = "SPSPR"
Me.TabCodes.Pages(0).Visible = False
Me.TabCodes.Pages(1).Visible = True
Me.cmbPR_Contr.SetFocus
Me.TabCodes.Pages(2).Visible = False
Case 3 '"WBS"
Me.RecordSource = "tbl_WBS"
Me.Caption = "WBS"
Me.TabCodes.Pages(0).Visible = False
Me.TabCodes.Pages(1).Visible = False
Me.TabCodes.Pages(2).Visible = True
Me.cmbWBS_Num.SetFocus
End Select
End Sub
Will this work? I was hoping to see the recordsource property with my table defined, but it remains blank.
Yes, this should work, but all tables should have displayed columns with the same names. If you have different names, create few subforms and change subform's SourceObject property on main form depending on iLogType
Yes, this should work, but all tables should have displayed columns with the same names. If you have different names, create a query on each table, having identical field names, expected by the form.
Such code runs on one of my Apps for 9 different queries.

MS Access 2007 VBA - Reusable list box code

I have a MS Access 2007 application which has several forms where I've used the same list box design. I have two list boxes, one of which gets values from a table with a query like:
SELECT id, value FROM table
And the second which is initially empty. In between these two list boxes are add and remove buttons, which are disabled by default. Clicking a value in the first list box enables the add button, and clicking a value in the second list box enables the remove button. Clicking the add button adds the selected item to the second list, and clicking the remove button removes an item for the second list.
The code I have for the add button is as follows ("ALLLIST" refers to the list with the query values, "SELECTEDLIST" is the one that's initially empty) :
Dim selectedId, selectedValue, safeValue As String
Dim existing As Boolean
Dim index As Integer
existing = False
selectedId = Me.ALLLIST.Value
index = Me.ALLLIST.ListIndex
selectedValue = Me.ALLLIST.Column(1,index)
'Loop through the list of selected values and see if this one has already been added to the list
For i = 0 To (Me.SELECTEDLIST.ListCount)
If (Me.SELECTEDLIST.Column(0, i) = selectedId) Then
existing = True
End If
Next i
'Only add the value if it's not already on the list
If (existing) Then
MsgBox "This list can't contain duplicate values", vbOKOnly + vbInformation, "Error"
Else
safeValue = Replace(selectedValue & "", "'", "''")
Me.SELECTEDLIST.AddItem (selectedId & ";'" & safeValue & "'")
Me.SELECTEDLIST.Value = Null
Me.REMOVEBUTTON.Enabled = False
End If
And the code for the remove button is:
Dim numItems, index As Integer
index = Me.SELECTEDLIST.ListIndex
'Remove the selected item and move to the top of the list
Me.SELECTEDLIST.RemoveItem (index)
Me.SELECTEDLIST.Selected(0) = True
numItems = Me.SELECTEDLIST.ListCount
'Cosmetic feature, select the row above the one we're removing
If (index > 0) Then
Me.SELECTEDLIST.Selected(index - 1) = True
Else
Me.SELECTEDLIST.Selected(0) = True
End If
'If the list is empty now, disable the remove button
If (numItems = 0) Then
Me.ALLLIST.SetFocus
Me.REMOVEBUTTON.Enabled = False
Me.SELECTEDLIST.Selected(-1) = True
End If
What I would like to do is, rather than copy and paste this code all over the place, have this template stored somewhere and then in my form code write something like:
hookUpLists(allListName, selectedListName, addButtonName, removeButtonName)
How can I do this? Can I write a module to do this some how? I'm also open to any improvements I can make on the above code.
Thanks
Thanks for the hint on using class modules, HK1, I'd never used them before.
To solve the original question, I created a new class module "MultiSelectListBox", which has the following code:
Option Compare Database
Private WithEvents allList As ListBox
Private WithEvents selectedList As ListBox
Private WithEvents addBtn As CommandButton
Private WithEvents removeBtn As CommandButton
Private numColumns As Integer
Public Sub hookUpLists(numberOfColumns As Integer, allValuesList As ListBox, selectedValuesList As ListBox, addButton As CommandButton, _
removeButton As CommandButton)
'Grab all the controls passed in
Set allList = allValuesList
Set selectedList = selectedValuesList
Set addBtn = addButton
Set removeBtn = removeButton
numColumns = numberOfColumns
'Tell Access we want to handle the click events for the controls here
allList.OnClick = "[Event Procedure]"
selectedList.OnClick = "[Event Procedure]"
addBtn.OnClick = "[Event Procedure]"
removeBtn.OnClick = "[Event Procedure]"
End Sub
Private Sub allList_Click()
addBtn.Enabled = True
End Sub
Private Sub selectedList_Click()
removeBtn.Enabled = True
End Sub
Private Sub addBtn_Click()
Dim selectedId As String
Dim existing As Boolean
Dim index As Integer
existing = False
selectedId = allList.Value
index = allList.ListIndex
'Loop through the list of selected values and see if this one has already been added to the list
For i = 0 To (selectedList.ListCount)
If (selectedList.Column(0, i) = selectedId) Then
existing = True
End If
Next i
'Only add the value if it's not already on the list
If (existing) Then
MsgBox "This list can't contain duplicate values", vbOKOnly + vbInformation, "Error"
Exit Sub
End If
Dim item As String
item = selectedId & ";"
'Loop over all of the columns and add them to the second list box
For i = 1 To numColumns - 1
item = item & "'" & Replace(allList.Column(i, index) & "", "'", "''") & "'"
'Don't add a trailing semicolon
If (i <> numColumns - 1) Then
item = item & ";"
End If
Next i
selectedList.AddItem (item)
selectedList.Value = Null
removeBtn.Enabled = False
'Select the next row
If (index <> allList.ListCount - 1) Then
allList.Selected(index + 1) = True
allList.Value = allList.Column(0, index + 1)
End If
End Sub
Private Sub removeBtn_Click()
Dim numItems, index As Integer
index = selectedList.ListIndex
'Remove the selected item and move to the top of the list
selectedList.RemoveItem (index)
selectedList.Selected(0) = True
numItems = selectedList.ListCount
'Cosmetic feature, select the row above the one we're removing
If (index > 0) Then
selectedList.Selected(index - 1) = True
Else
selectedList.Selected(0) = True
End If
'If the list is empty now, disable the remove button
If (numItems = 0) Then
allList.SetFocus
removeBtn.Enabled = False
selectedList.Selected(-1) = True
End If
End Sub
Most of the above is identical to what I was already using, one important thing to note for anyone stumbling across this is the use of "WithEvents" when declaring the variables. This tells Access to look in the class module for event handlers. Finally, from my form I can do the following:
Private contactList As MultiSelectListBox
Private Sub Form_Open(Cancel As Integer)
Set contactList = New MultiSelectListBox
contactList.hookUpLists 3, Me.allContactsList, Me.selectedContactsList, Me.addContactBtn, Me.removeContactBtn
End Sub