Multiple Text Box and Lookups - vba

I am currently trying to improve on an Access Database VBA that I have inherited from my predecessor at work. I have come unstuck on a particular form.
I have a form that at the moment is just a large form containing 32 individual textbox, with the same code behind each but it is the same code repeated for each textbox with just the references to the text box changing in each.
Private Sub Cand_No2_AfterUpdate()
Cand_Name2 = DLookup("[Name]", "[qryExamAbsences]", "[Cand_No] = Cand_No2")
End Sub
Then once the button is pressed
If Not IsNull([Cand_Name1]) Then
Rope = Rope & " Or Cand_No = " & [Cand_No1]
End If
(The If statement is contained in the button mousedown event.)
Occurs for each text box which then filters a report that is printed for office use. There are many problems with this but the major one I am trying to solve is that there is an upper limit to the number of entries, if I need to filter more than 32 I would need to delete the text and start again as it were.
Is there a way of combining all this into a single section of code which will create text boxes when needed?
EDIT.
I have found a way to give the impression to the user that the text boxes are being created after each entry which has improved the form from a user standpoint (no longer having 32 textboxes or having to scroll down to the Print Button.) however this still hasn't solved the issue of messy code as I have had to repeat the extra code for each box again, it also leaves me with the maximum of 32 entries still.
The new code is as follows:
If Not IsNull(Cand_Name1.value) Then
Cand_No2.Visible = True
Cand_Name2.Visible = True
cmdPrint.Top = 2500
cmdPrint.Left = 2500
DoCmd.MoveSize 1440, 2201, , 4000
Else
Cand_No2.Visible = False
Cand_Name2.Visible = False
cmdPrint.Top = 2000
DoCmd.MoveSize 1440, 2201, , 3500
End If
Essentially makes the next text box visible and moves the print button down to make room for the new text boxes. It also expands the window.

Could you not just have 2 text boxes, one for CAND_NO and another for CAND_NAME and then beside those two boxes place an ADD CAND_NO button.
Create a list box that would list every CAND_NO / CAND_NAME after they press the add button so they can see what they've added so far. Then loop through your list box to build your rope string or have your rope string either a global variable on the form and build it as they add numbers or stored in a hidden text box storing the value as they add numbers if you don't like global.

Related

How to use VBA to show / hide multiple combinations of tables in MS Word? - explained further within

I have a word 2010 document with some basic VBA code attached to a number checkboxs that show / hide a sections throughout the document.
There are a number of these throughout the document however they all share the same basic code which appears to be working well enough (example below)
Private Sub PlanningBox_Click()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
End Sub
The issue i am having is that some of these sections then have checkboxes within them that show / hide further section which is fine, however if the user unchecks the first initial checkbox (hiding all sections) then re-checks it, it will open up the initial section again but not the ticked subsections - in order to do this they have to uncheck/check the subsection again. I know this seems minor but i need to ensure the form is as fluid and user acessible as possible..
For example let's say there is a single row table with the text 'Have you spoken to anyone for feedback?' (checkbox1) - the user will tick the check box which will unhide a section of text below giving directions, asking some questions, general blurb etc. At the bottom of this is another question 'Who did you speak to?" - and then multiple checkboxes 'mother','father', 'Child' (checkbox A,B,C,etc). Checking any of these boxes opens up a table with additional questions for each one selected, the user can check any number / combination of the checkboxes.
Now if the user unchecks the initial checkbox 1. all sections will be hidden no problem, but then if they then check it to reveal the section again the mother, father etc boxes remain ticked but will not reveal the sections without needing to be unticked/ticked again. Is there a way so that ticking the intial box will also reveal any previously unhidden sections again?
My knowledge of VBA is very limited, i have considered using If + ElseIF statements but my understanding is that i would need an ElseIf for every potential combination. Is there a more sophisticated way around this at all?
Hopefully i have articulated this well enough but happy to provide further information. Thank you for any assistance given
You could create a common routine that evaluates every checkbox and sets the corresponding section accordingly. Call that one routine whenever a checkbox is clicked.
Private Sub PlanningBox_Click()
SetRangeVisibility
End Sub
Private Sub SomeOtherBox_Click()
SetRangeVisibility
End Sub
Public Sub SetRangeVisibility()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
If SomeOtherBox.Value = False Then
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = False
End If
' Etc...
End Sub
Further, if you use an array of check boxes you'd only need to write a single SomeBox_Click event procedure. Here is some information on Control Arrays in VBA.

Excel vba, unable to select row in listbox

I have a page in a multi page form which takes input from a text box and check box and adds this into a multi column list box below it when a submit button I clicked. I then want the user to be able to select a row in the list box and display the contents in the text box and check box for editing.
I can add multiple text box and check box values into the list box using the submit button, but it does not select a row when I click on the list box item. It just does nothing. It looks like the focus is not being transferred.
Would really appreciate it if someone could give me some ideas or guidance on how I may solve this? The code for the two actions are:
Private Sub cmdaddcontrol_Click()
If Not txtbox_Ctrl_Desc = "" Then 'check for input into text box
ctrl_listbox.AddItem 'add items to list box
ctrl_listbox.List(ctrl_list_count, 0) = ctrl_list_count + 1 'add row number in list box column
ctrl_listbox.List(ctrl_list_count, 1) = txtbox_Ctrl_Desc.Value 'set list box column to text box contents
If Not chkbox_ctrl = False Then 'check if check box is selected then insert appropriate value into listbox column
ctrl_listbox.List(ctrl_list_count, 2) = "Y"
Else
ctrl_listbox.List(ctrl_list_count, 2) = "N"
End If
ctrl_list_count = ctrl_list_count + 1 'increment listbox row counter
txtbox_Ctrl_Desc = "" 'reset text box to blank
chkbox_ctrl = False 'reset check box to blank
Else
MsgBox "You have not enetered anything in the control box" 'error message if no control description in text box
End If
End Sub
Private Sub ctrl_listbox_Click()
Dim i As Integer
'find the selected list item
i = ctrl_listbox.ListIndex
ctrl_listbox.Selected(i) = True
'add the values to the text and check boxes above the list box
txtbox_Ctrl_Desc.Value = ctrl_listbox.Column(1, i)
If Not ctrl_listbox.Column(3, i) = " Y" Then
chkbox_ctrl.Value = True
Else
chkbox_ctrl.Value = False
End If
End Sub
After finding the cause of the issue in the comment chain of the question, I'd like to persist the solution here as an answer for the people that might come across the question in the future:
Make sure that the .Enabled and .Locked properties of the misbehaving ActiveX control are set correctly. (True if the control should allow manipulation/activation by the user at runtime. False if you want to deny the user interaction with the control.)
Verify that the event procedure has the correct name. Maybe you changed a controls name? The correct naming scheme for event procedures is controlName_eventName(), e.g. ListBox1_Click(). An empty _click() procedure is generated when you select Show code in the control's context menu in Design Mode.
Although seemingly trivial, sometimes simple details escape our attention - even more so in complex projects. It's often helpful to have someone from outside the project have look at your problem. In their innocent ignorance of your project's inner workings, they might just be able to ask the right questions that lead to a quick solution.

Write individual listbox items into different text boxes and repeat until all text boxes are full

I'm programming in Visual Basic.
I have one form.
Form 1 contains:
nameTextBox
addNameButton
namesListBox
generateButton
week1TextBox
week2TextBox
week3TextBox
week4TextBox
The user needs to go to Form 1 and type a name in the text box, then add the name to the List Box. The user will add 4 names to the List Box. So, the ListBox will contain the names: Adam, Brenda and Carol.
When the generateButton is clicked, the 3 names have to be written to the text boxes in that order. So week1TextBox should contain "Adam", week2TextBox should contain "Brenda", etc... but once the last name (in this case "Carol") is written into the text box, the loop should start over. Ultimately, there may be up to 50 week text boxes (so week50TextBox). So the loop needs to repeat over and over.
As there is a lack of source code in your question, I'm really not sure exactly how the layout should look, I can only offer some advice/suggestions.
I would recommend creating your listbox control, input textbox, and button to add names to the listbox. In addition to these, though, also add a scrollable panel. (Not sure what the exact term for that control is in VB.net; it's been a long time since I've worked with that language.) Because it sounds like there might be a variable number of items on the panel, when the user goes to generate the list of names, I would use the following rough pseudocode:
Dim OutputTexts As New ArrayList ' This is only here if you want to work with these textboxes later
Private Sub CreateOutput() Handles btnGenerate.Click
pOutputPanel.Controls.Clear()
OutputTexts.Clear()
Dim NextX As Integer = 0 ' Pretty much unnecessary value, but included in case you want to mess with this
Dim NextY As Integer = 0
For i As Integer = 0 To Convert.ToInt32(txtWeekCount.Text)
Dim txtName As New TextBox
txtName.Text = lbNameList.Item(i Mod lbNameList.Items.Count)
txtName.Location = new Point(NextX, NextY) ' Play with this as necessary
NextY += 50 ' Play with this as necessary
OutputTexts.Add(txtName)
pOutputPanel.Controls.Add(txtName)
Next
End Sub
Again, this is very much pseudocode, so I would not encourage copying and pasting, but give it a read, make sure you understand all of it, and then try implementing something similar. There might be an easier way to do it, but I have not programmed in VB.NET in probably over 2 years (at least). Nonetheless, the most important thing in here is the following line: lbNameList.Item(i Mod lbNameList.Items.Count). By Mod-ing your indexing variable, you will be accessing items sequentially, and then repeating from the start of the ListBox items collection once i is out of range.
I would also encourage you to dynamically generate your TextBox controls as needed rather than manually adding in 50 or more TextBox controls.

Number Picker in Access / VBA

I am trying to put a number picker in a form in MS Access 2007. Here's an example of what I am trying to make:
I cannot find this in the default form controls, and have tried to make one myself using a listbox. Listboxes can be modified to look just like the number picker above, however the arrows only change the view, of the list, and not the actual selection (that is the value). For example, with the list box, if I have it range from 1 to 3, and default at 1 - when I change it to 2 via the arrows, the value of the listbox does not change, and is still one.
Does anyone know how to get a number picker in Access?
So you want to create a list of numbers and allow users to change the value displayed (AND stored as the control's value) using up and down arrows, such that they select the next or previous in the list.
I would suggest creating a text box and two buttons. Populate an array with the list of values. When a button is clicked it would:
A. Find the position in the array of any value already entered into the text box (eg loaded from a database)
B. Get the next or previous item from the array.
The array is populated as required (probably when the form is opened).
If you just need to allow the user to enter a whole integer number (ie a number spinner) you would do as follows:
Create one using a (locked) textbox and two buttons. Just add a textbox (name it something like txtValue) and two buttons (btnUp and btnDown), then add code like this to the Click event of those buttons:
Private Sub btnUp_Click()
Me.txtValue = Nz(Me.txtValue, 0) + 1
End Sub
Private Sub btnDown_Click()
Me.txtValue = Nz(Me.txtValue, 0) - 1
End Sub
You could add if statements to limit the data being entered
Or you can use a 3rd party control.
http://www.fmsinc.com/microsoftaccess/controls/components/spin-button/index.html
There are probably more, but be aware that using these sorts of controls in Access is unsupported, and there is no guarantee moving forward that they will work in Access. You're far better off using the native methods described earlier.

Combobox event listener

I'm trying to use the solution given to this issue : Programmatically create event listener in VBA in my particular case :
I programmatically create comboboxes. What I would like to do is to programmatically create a single textbox with a precise label or several texboxes with as many labels next to the generated comboboxes depending on their values.
Here is the code I use to programmatically create those comboboxes :
Set listBoxB1 = Frame1.Controls.Add("Forms.ComboBox.1")
With listBoxB1
.Name = "list" & i
.Height = 15
.Width = 100
.Left = 70
.Top = 10 * i * 3
.AddItem "NUM"
.AddItem "LIST"
End With
So I want that when the user choses the value LIST, a single textbox is displayed next to the combobox with a label and when he or she uses the value NUM, 6 textboxes are created next to the combobox and aligned with it horizontally.
I actually want that the display changes automatically when the user changes the value of the combobox, it would be helpful if you can give some indications about that.
Can you please tell me how I can adapt the solution in the link above to my case, because I'm very new to VBA and don't exactly know how to do that, I have tried to implement the code given but failed.
It really is somewhat involved. This question discusses it: Assign on-click VBA function to a dynamically created button on Excel Userform . The accepted answer contains a trick that I have used many, many times. Create all the controls you will ever need ahead of time (there is only a finite amount of real estate on a form so in practice this will be a reasonably small number) and make them visible (or hide them) dynamically as you need them by controlling the .visible property. It is even possible to have 1 control sit on top of another in the design view with only 1 visible at run time. The visible one at run time will receive the events.