ms access dynamically adding textbox for amount of goals - ms-access

Good evening,
I'm completely stuck in ms access, trying to create dynamically added fields.
I've got a form called frmMatch.
It contains 6 fields called:
MatchDate - Date field
CompetitionType - ComboBox
Location - Input field
TeamName - Input field
ResultHome - Input field
ResultAway - Input field
Now what I would like to create is just a simple button called something like Add Scored Players
The problem is, I have no clue on how to get VBA to run through the ResultHome field if it is a Home game or through ResultAway if it is an Away game.
For example when a home game end up with a 3-1 win I would like VBA to run through a loop until it hits the ResultHome value, in this case 3.
And adds the amount of text box in according with the ResultHome value.
So I can put in the names from a combobox or textbox and the time he scored.
I hope you guys can help me out with this.
Ive searched on this form as well as other website but I cant find anything that would help me.

What you need here is not to "add textboxes", but rather to make them visible.
So, what you'll need to do is to create all your textboxes and set their Visible property to "False". Name them with numbers, like "tbScore1", "tbScore2", "tbScore3", etc...
Then, you'll need to add some VBA behind your button to make the proper number of textboxes visible. Something like:
'If both scores are 0, no need to show anything
If ResultHome.Value = 0 and ResultAway.Value = 0 then
exit sub
else
'Otherwise, set the value of the loop to whatever the score is
If ResultHome.Value <> 0 then
LoopVal = ResultHome.Value
else
LoopVal = ResultAway.Value
endif
endif
'Make as many textboxes visible as necessary
For i = 1 To LoopVal
MyFormName.Controls("tbScore" & i).Visible = True
Next
Note: this is all "aircode" and is not tested, so it may require some tweaking to make it work. But this is the logic structure you can use to achieve your results.

Related

Paste a value in a textbox on the second tab of a navigation form access vba

I'm quite new to VBA and I've been looking around but cannot seem to find a solution to my problem.
I have made a navigation form (frmNavigation) with 3 buttons, each referring to a different form, let's call them frm1, frm2 and frm3. In the navigationform the control buttons to switch between tabs are all named differently (btn1, btn2, btn3), but the subform that shows either frm1, frm2, or frm3 has the same name: “NavigationSubform” (this shows a different form depending on which tab is clicked on, based on the 'navagation target name' referring to frm1, frm2 and frm3).
When I want to refer to a textbox (txtBox1) on form 1 (first tab) and insert a value i can do this by:
Forms!frmNavigation!NavigationSubform.Form!txtBox1.Value = "insert awesome text"
But how would I refer to txtbox10 on the second tab (frm2)? Just using the following does not work:
Forms!frmNavigation!NavigationSubform.Form!txtBox10.Value
You then get the error 2465 (can't find the field).
I’ve been trying many different things, but can’t seem to get it right. So how do I refer to a textbox on a different tab than the first one?
Help us much appreciated!
Only one subform can be loaded at once. So you've just got to break this process into two steps.
Store the value from txtBox1 somewhere outside of the NavigationSubforms (a textbox on the parent form with visible = no, a global variable or a table works).
In frm2's On Load event, set txtbox10 to be the value you stored.
Just note, that you will need to add conditions in the On Load event if you want to avoid that textbox being set to an empty string or a wrong value if you have a setup where your filter is changing.

How to check whether a combobox selection contains specific text or word

My combobox has 5 values, and three of them contain a particular word that, if selected, should enable two other controls on the form that would be otherwise disabled. I already figured out how to enable/disable the controls but im trying to figure out the correct syntax for the code that would check if these values are selected or not, but cannot find a clear format of code. Please help.
If your combobox only has 5 values and is limited to those 5 values, then just use the whole value and not the containing word.
private sub cmb1_AfterUpdate()
if me![cmb1].value ="value1" Or me![cmb1].value ="value2" Or me![cmb1].value ="value3" then
me!txtbox.enabled = true
else
me!txtbox.enabled = false
end if
end sub

Autofilling my form in Access with the use of a Combo Box

I'm having a problem when I want to autofill my form in Microsoft Access. The idea is that I use a combo box to select a name. Then the onChange code of my Combobox automaticlly inserts all the other data in the proper field. I use this code on the Combo Box.
Private Sub cmbName_Change()
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(2)
Me.tbBirthday = Me.cmbName.Column(3)
End Sub
This methode works fine for the personalnumber and the emailadress. But it doesn't work for the birthday date, it returns a null value but when I check my table there is is a date in the proper field.
Am I missing something? I tried everything but it wont work.
I was thinking that the problem is related to the birthday column being the last in the table. Or having the date type.
Thank you in advance for your time and efford!
Edit; The .Column(1) is missing because this is the name that is already inserted with the ComboBox.
There is some confusion caused by the wording of the question, I'll try to state back how I've interpreted and if I have it right it may lead you to an answer.
You have combo box called cmdName that is pre-populated with data from a table. The content of the combo box could look as below (you may have set column widths to zero to hide the data)
0001|Gary Evans|gary#email.com|01/Jan/1970
0002|J Rommers |JR#email.com |02/Jan/1970
When the user selects J Rommers Me.tbPersonalNumber is populated with Me.cmbName.Column(0) (0002) and Me.tbEmailadress is populated with Me.cmbName.Column(2) (JR#email.com) but Me.tbBirthday is not being populated with Me.cmbName.Column(3) (02/Jan/1970).
Assuming Me.tbBirthday is a text box with no code that might clear it out, I suspect the issue is within the combo box. Not being sure how your combo box is set up, I would suggets the following checks:-
In the combo box properties, does the Column Count equal 4?
In debug, with a breakpoint on Me.tbBirthday = Me.cmbName.Column(3), does it show you the date you are after?
If it is not there does the query that populates the combo box have it in?
Edit based on comments to help further: -
Change the query to SELECT Personel.PersonalNumber, Personel.Emailadress, Personel.Birthday, Personel.Name FROM Personel ORDER BY Personel.Name; this puts all the fields you want hidden at the front.
Change the column widths property of cmbName to 0,0,0, this first the ones you want hidden and leave the last one to fill the width of the combo box.
Ensure the column count property is still 4 as per the answer
Change your code as per below and Gustav's answer
Replacement code:-
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(1)
Me.tbBirthday = DateValue(Me.cmbName.Column(2))
This accounts for the fields moving in the query and ensure the date shows as a date like you wanted.
Comboboxes (and Listboxes) always return a string, so convert that to a Date value:
Me!tbBirthday.Value = DateValue(Me!cmbName.Column(3))

Update Display for Combo Box

I have a form with multiple combo boxes, these boxes have 2 column list where column 0 width is zero. All these boxes have row sources defined for their drop down list values.
When I try to over ride the display value of the combo boxes via vb code, it works for few of the controls while doesnt do anything for the rest. So I made an isolated code just to update values of all combo boxes(cbo) and again the same controls pass/fail. Here is the code I am trying to work with, can anyone please tell me why I am unable to update some combo boxes while others work fine?
Thanks!
Sub test()
Dim Ctrl As Control
Dim CtrlName_Combined as String
For Each Ctrl In Forms("frmNewTransaction").Controls
'Name of all combo boxes start with 'cbo'
If Left(Ctrl.Name, 3) = "cbo" Then
Ctrl = "Hello"
CtrlName_Combined = ctrlName_Combined & Ctrl.Name & " "
End If
Next Ctrl
MsgBox CtrlName_Combined
End Sub
Here is screenshot of the form after code run (All Combo boxes start with cbo):
Thank you guys for the help. The problem started when few of the combo boxes did not take value when i executed:
forms("FormName").Controls("ComboBoxName")="Some Value which was in the list"
So I tested all my combos by passing them value "Hello" which I know was not in their row source. Strangely so few combos responded and others did not. This was the rabbit hole as all my combos were configured same apart from their row source queries but all had an ID and eName column.
Problem was that few combos were not taking the text values I passed them but turns out that if I gave them Numeric value according to the first column they start responding (what i dont get here is that how some combos behave one way and rest the other way)
Anyway this helped me get past the problem, thought i should share this after bothering you all with it.
Also I am seeing that the question or manner was not well received by the community, would be helpful if someone pointed out what not to do for future references
Thanks Again!

Way to add a calculated number of X's to a form input?

I have certain product codes with varying number of letters/digits e.g. 53HD6J, HH88WBD3 (varies between 5 to 10 letters/digits). In order for our barcode to scan these correctly there has to be 13 letters/digits. I don't want to make the user to input -XXXX after each code but rather have Access calculate the difference between 13 and the length of the code and fill the remaining with a X's. Is this possible either by vba or and expression?
I currently am using about 6 IIFs in one formula to fill remaining blanks with X's but hoping there is an easier way.
I have a form to enter in the batch number (product code). Once that form is submitted it links to a report that is printed. On the report are those batch numbers (53HD6J, HH88WBD3). The spot I want to have this feature is in a text box right next to the codes where Access determines the length of the codes and computes the remaining X's to add. This is in barcode font so this text box is where the 53HD6JXXXXXXX would go. Hope that clears it up!
So I have that part figured out. My problem now is my barcode font reads the text no matter what and translates it still so barcode shows up when the batch number is blank (I have four spots for batch codes to be inputted). So what I had before was =IIf([Text31]="",""&[Text31]&"","") which seemed to work. Hopefully I can continue this with the new formula. If that's unclear let me know.
**(The "" & & "" is so the barcode can be scanned).
My formula was wrong right above with the IIf. I figured it out! Forgot I had used ' Like "*" '. Thanks!
You can do what you want with String() and Left().
Here is an example from the Access Immediate window:
product_code = "53HD6J"
? product_code & String(13, "X")
53HD6JXXXXXXXXXXXXX
? Left(product_code & String(13, "X"), 13)
53HD6JXXXXXXX
Based on the update to your question, I think you can use that approach for the Control Source of a text box where you want to display the "expanded" product code.
Pretend your report has a text box named txtProduct_code where the raw product code, such as 53HD6J, is displayed. And there is a second text box where you want to display that value with the required number of X characters (53HD6JXXXXXXX).
Use this as the Control Source property of that second text box:
= Left([txtProduct_code] & String(13, "X"), 13)
Alternatively, you could make it a field expression in the report's Record Source query.
SELECT
product_code,
Left(product_code & String(13, "X"), 13) AS expanded_product_code
FROM YourTable;