How to assign the next incremented alpha-numeric string value in a text box using VBA Access? - ms-access

Currently, I have a datasheet that contains the following data as an example:
FieldOne FieldTwo
TestOne.01 TestOne.01.01
TestOne.01 TestOne.01.02
TestTwo.02 TestTwo.02.01
TestThree.03 TestThree.03.03
TestFour.02 TestFour.02.02
TestFour.02 TestFour.02.03
I have a form that contains a Combo Box. The values of the combo box is all the values in FieldOne. I also have a Text Box on the same form where I would like, upon selecting a value from the Combo Box, assign the next incremented value in FieldTwo of that specific record into the Text Box.
For example, if I select from the Combo Box, the value TestThree.03, I would like to populate the Text Box with TestThree.03.04. If I select the value TestTwo.02, the Text Box should be populated with TestTwo.02.02
What I've tried to get started with so far:
Dim frmDS As SubForm
Dim criteria As String
Set frmDS = getSubForm("form", "datasheet")
criteria = "FieldOne = '" & Me.comboBox & "'"
With frmDS.Form
.RecordsetClone.FindFirst criteria
If Not .RecordsetClone.NoMatch Then
MsgBox .RecordsetClone.Fields("FieldTwo")
End If
End With
However I face 2 difficult problems:
If I select TestOne.01, since there are 2 records containing TestOne.01, it will display TestOne.01.01 instead of the last value TestOne.01.02
I am not sure how to extract the number after the right-most decimal point in the value of FieldTwo, increment it, and append it back to the value in FieldOne so that it can be assigned to the Text Box
I'm not sure if my initial approach is heading in the right direction anyways.
How can I overcome these issues?
Thanks

For question 1, the answer is simple
Instead of .RecordsetClone.FindFirst criteria
use .RecordsetClone.FindLast criteria
For question 2, you just need a bit of VBA code to first get the last code and then format it back to append.
You don't give all the details of your formatting so I'll assume the simplest solution - that is that the last code is 2 digits long. If not you can use a variation of Instr and Mid or Right functions to find the last code
Example:
Dim intLastCode as Integer
Dim strLastCode as String
With frmDS.Form
' Get last two digits from Field 2
strLastCode = Right(.RecordsetClone.Fields("FieldTwo"),2)
intLastCode = CInt(strLastCode) + 1
' Format and append incremented value to Field 1
MyTextBox.Value = .RecordsetClone.Fields("FieldOne") & "." & Format(intLastCode,"00")
End With

Related

ComboBox Access Match on ID but Display Category

I am working on a project but have run into a wall with Access's combo-boxes. I've attached an image so things (hopefully) make more sense when I describe my situation.
I have two combo-boxes on a form, the left one is called cmb_body_system, and the right one is called cmb_icd9code.
What I am trying to do is have the left box display three options, the three from the body_system_type table: infectious, circulatory, respiratory. From there, the second box will display the matching description in the ICDLight table column titled icd9Description.
I have gotten as far as getting the left box to display the bodyID's associated with each matching Icd9Description, but I can't get further than that. The VBA that I have on these is:
Private Sub cmb_body_system_AfterUpdate()
cmb_icd9code.RowSource = "SELECT ICD9Description FROM ICDLight WHERE ICDLight.bodyID = " & Me.cmb_body_system & ";"
End Sub
And as you can see, it's working, but I want to go that one step further and don't know what/how to search for this exactly. I mean, I don't even know how inaccurate/accurate the title is for that matter.
I think you need to rephrase your question. From your question title, I think this is what you need.
Select your cmb_body_system
Go to property sheet
Under Format tab:
Column Count = 2
Column Widths = 0cm; <- there is a semicolon
Under the Data Tab
Bound Column = 1
Row Source Type = Table/Query
Row Source = "SELECT * FROM BODY_SYSTEM_TYPES"
Now your combobox will show "bodysystem" as display text and Bodyid as value

Referencing list boxes in Access

I'm new to Access so this might be a simple question.
I have a form in Access 2013. There is a subform displaying a table from an SQL server, like so.
Company Product
-----------------
CompanyA Product1
CompanyA Product2
CompanyB Product1
CompanyB Product2
Using ListIndex in a list box, I can display the index of any row I click on. For example if I click on the second row (CompanyA, Product2) the list box shows a ListIndex of 1. If I click on the third row the ListIndex is 2.
How do I get a list box to display the value of a column instead of the ListIndex?
What I am trying to do is that, when I click on a row in the subform, I'd like to display each column value for that row in its own list box.
However, I cannot seem to use ListIndex as a variable in a larger function. I've attempted the following:
Typing only the column name into the list box. Does not update the value if I click on a different row.
Column property does not update the value if I click on a different row.
Controls property gives an error.
Value property displays the correct row but only works for the first column.
Combo boxes circle back to the problem that I need to use ListIndex as a variable.
Is there a different property I should be using? Am I missing something in the properties I tried?
There seems to be a little confusion with terminology. Your List3 is a list box, not a subform.
The fields Company and Product look like text boxes, but if the first one has the control source =[List3].[ListIndex], and shows a text and not a number, it seems to be a listbox with height = one line.
I suggest using text boxes for Company and Product, with these control sources:
=[List3]
for the bound column. Alternatively, for consistency: =[List3].Column(0)
=[List3].Column(1)
for the second column.
These text boxes update themselves automatically when you click on an item in the listbox.
To get the value of a listbox, and if you allow multi-select, use the following example:
For Each varItem In Me.lstHierarchy.ItemsSelected ' Loop through the items selected (if multi-select allowed)
strPrint = ""
For i = 0 To iCols ' Loop thru each column in a row
strPrint = strPrint & "|" & Me.lstHierarchy.Column(i, varItem)
Next i
Debug.Print "Selected Value: " & strPrint ' Display the row; columns are delimited with '|'
Next

Access Dynamic IIF reference on form

I cant seem to figure out how to reference a combo box value on a form in another textboxs sum iif function. The sample of what I am trying to do is below, with the reference to "TestCombo" the value I want to change based on who is selected. I tried using CHR(34) quotes, and I cant seem to figure it out.
SUM(iif([SaleStatus]= 'Pending' And [SalesAdvisor] = [TestCombo],1,0))
This same function works if I do the following
SUM(iif[SaleStatus]= 'Pending' And [SalesAdvisor] = 'TestAdvisor',1,0))
That led me to believe I needed quotes, which I expected, but that doesn't work either, such as the example below.
SUM(iif"[SalesStauts]= 'Pending' And [SalesAdvisor] = " & chr(39) & [TestCombo] & chr(39) & "",1,0))
Can somebody help me use the combo box as a dynamic reference to the sum iif?
Are you sure that your combo is equal to 'TestAdvisor'? It is quite common to have a numeric bound column and a text display. If you have more than one column in the combo, you will need to refer to the column property to get the text column. If you include the RecordSource for the combo in your question, it will be easy to see.

autopopulating text boxes from combo boxes

I have created a form to input new work orders in access. I want the text boxes in my form to populate from the selection in the combo box.
The combo box lists serial number for equipment, and once a serial number is selected, I want CompanyName, PurchDate, LastMaintenanceDate, & ModelNo to populate.
I saw an example of dlookup and tried this expression on the Company Name textbox:
=DLookUp("[CompanyName]","SerialNoInfo","[SerialNoInfo].SerialNo= " & Str([Combo341].[Text]))
I keep getting an error #Type or #Error in the box depending on what I do. When I try to build a code for the event on update, nothing happens at all. I have no idea what I am doing wrong.
You can alternatively use :
Private Sub COMBOBOX_Change()
me.TEXTBOXNAME.Text = me.COMBOBOX.Column(X)
End Sub
Where TEXTBOXNAME is the the textbox you want to fill
Where COMBOBOXis the the combobox from which you want to get data
and X is the number of column with data (starting from 0 )

combobox cascade effect not working for different rows

I have two comboboxes on a subform.The first combobox is used to populate the second combobox. These are placed in the detail section of the form. I want them to work this way:when I select any value from the first combobox, I want the second combobox of the same row to get populated by relevant value.
As of now, I have tried to implement this and as I select any value from the first combobox of row 1 I see the second combobox of the same row gets populated but as I go on selecting values from the first set of comboboxes I see that the values in the second set of the comboboxes above changing or becoming null.
Here's the code:
The 1st combobox is cboRCMTask:
Private Sub cboRCMTask_AfterUpdate()
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
Me.cboRCMTaskOptions.Requery
End Sub
cboRCMTaskOptions is the second combobox.
The form_current event:
Private Sub Form_Current()
Me.cboRCMTask.RowSource = "SELECT ID, RCMTask FROM tblRCMTask;"
If IsNull(txtRCM_ID) Then
Me.cboRCMTask = Me.cboRCMTask.ItemData(0)
End If
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
If IsNull(txtRCMOption_ID) Then
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
End If
End Sub
From your description, you are using a continuous form. While it looks like a continuous form has many rows, from the point of view of coding you can consider it to have just one row, the current row. I suspect that the control source for combo 2 is a hidden, numeric column in the combo, when you change the row source for the combo, the visible row can no longer be found, so it cannot be displayed. You will either have to provide a pop-up form for editing, or a textbox to store the value for the form and a slightly dodgy combo to edit that value. You can do a little to improve the appearance with conditional formatting.
in your first piece of code, shift the code to the on_click event. I am not sure what you are trying to achieve with the reference to ItemData but I think it is unnecessary, comment out that line.
Similarly the third to last line in the Form_current event, replace with a requery.