Access VBA reselect items in List Box - vba

I'm working on a DB where the users have to select items from multiple list boxes, at the end of the all of this, the form closes and opens a new form. All of that works great.
My issue is that when the user goes back to the original form, I want it to load in with their list box items already highlighted. Sort of a "here is where you left off" kind of thing.
I have code that works, but it's somewhat limited:
If InStr(me.Backup_Chain, "Apple") > 0 then
Me.List_Type.Selected(0) = true
End if
If InStr(me.Backup_Chain, "Grapes") > 0 then
Me.List_Type.Selected(1) = true
End if
"List_Type" is just a textbox that I populate with the users previous selection. So if they selected Apples and Grapes it reads: 'Apples','Grapes'
This works pretty well, but it's not dynamic. So if I add "Bananas" to the list, Bananas because item number 1 in the list, and thus screws up all of my other references.
So, my question is 2 parts:
Is there an easier way or reselecting the list box items?
If not, is there a way to make the Selected(#) dynamic? i.e. to tell that "Grapes" is in location 1 vs 2.
Thanks everyone!
Edit: In addition to this, I have a run a query to filter one of the list boxes down to a subgroup of clients (based on the other list box selections). Which means I need to refresh the page. So I can't get by with just hiding the form.

Related

Excel-VBA UserForm ListBox - How can I Highlight option without Selecting

Nutshell:
What is the equivalent to Me.ListBox1.Selected(2) = True but for highlighting instead of Selecting?? (Highlight might be the wrong word but I'm sure I've seen that before in this context - the outline box around an option instead of actually selecting it)
** Tried **
Me.ListBox1.ListIndex = 2
doesn't do anything either.
With Me.ListBox1
saveVal = .List(0)
.Value = ""
.Value = saveVal
End With
Gives me an error saying "Invalid Property Value"
Background:
Why do I need to click twice on some items/boxes?
I have a userform with several ListBox which are populated by adding items one by one, which works fine.
The ListBox settings have been changed to MultiSelect = 1 - fmMultiSelectMulti and ListStyle = 1 - fmListStyleOption
The issue is when trying to select an option on some boxes for the first time, it just highlights it without selecting it, while on other boxes and other tries it may or may not select an option straight away. It doesn't seem to be the same culprits and is a bit random - does it have something to do with where I'm clicking on the option? On the item text instead of the item box? Why does it work on some and not others?
After I've selected one option in a box, it then allows me to select any other with a single click which is the desired behaviour.
I found a link where someone had suggested this behaviour was because of a corrupted workbook, but I don't think it's that. I just created a new book with 1 listbox in a userform, changed to desired settings, copied the listbox 6 times, put the listsource as rand() in columns A:G and I'm still getting the same issue.
Edit: My plan is to "highlight" the top option on each box so that the first click on a box has no option other than to select (because something is already highlighted)...
The trick to tackling my INITIAL problem seems to have been after populating each list to set .ListIndex = 0 but this only worked after using .SetFocus
Solved! (For now, I think)
Even though this doesn't highlight anything it allows me to select something first time without fail.

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.

MS Access 2003 - Automatically show last records in list box on a form rather than first

So I have a form that has a listbox that shows like a ledger. My question is how can I make it display the last records (or have the scroll bar default to the bottom instead of the top), instead of the first few as the default.
Now I don't mean reversing the order from bottom to top instead of top to bottom (though that would be a cool thing to learn how to do), just simply having the bottom of the list (in terms of the scroll bar) shown and the default, so that it is always showing the last 10 or so records (based on the size that I made the list box).
So i think this is simple, but then again, I obviously do not know?!?!
Thanks!
In a suitable event, such as the current event:
Me.ListX.Selected(Me.ListX.ListCount - 1) = True
You could add some code to the form load event so that it will do this:
YourListBox.SetFocus
YourListBox.ListIndex = YourListBox.ListCount - 1
YourListBox.Selected(YourListBox.ListCount - 1) = False
It basically selects the last item in the list box so it will scroll down to it, and then unselects it.
I know this is later but maybe this will help someone in the future who comes upon this thread. This is the code I used to go to the last record then unselect the last record.
YourListBox.SetFocus
YourListBox.Selected(YourListBox.ListCount - 1) = True
YourListBox.Selected(YourListBox.ListCount - 1) = False
How did you set the listbox items? Are they from a database? If yes, then you need to update the SQL statement with an "order by columnName".

choosing the right control for developing windows application

I am looking for a contol either listbox or listview to support my requirements.
Basically how my application looks is:
alt text http://img51.imageshack.us/img51/451/39967846.png
The background should be black
when the user clicks any 'row' the
row should get highlighted witgh
grey.
The user will have the ability to
search items in this control.
For example , if one of the row
displays 'This is the second Item in
this car'
If the user searches for Car, that corresponding row(s)
should get
highlighted with pink.
The user should be able to search an item in this contol and specify a number to display the lines below the searched row.
For example, if the user Searched for 'car' 5, the rows that have car should get highlighted and their immediate 5 rows.
I mean
Search Result :
car row
next row 1
next row 2
next row 3
next row 4
next row 5
car row
next row 1
next row 2
next row 3
next row 4
next row 5
The user
should be able to drag and drop the
files and they should get opened in
this 'control' i.e each row should
display a new line till the EOF.
The user
will have the ability to right click
on the row, context menu pops up
displaying that row item.
The contol
should be able to display specific
text items in a row(s) with
different color.
Having said these things. I used to program in VC++6, VB6. But using those version now seems not realistic now as I lost touch. But if I have to use the latest 2008 versions, for which I do not have any experience seems scary.
So, If you are reading this , and you have got some experience in these fields or using controls that support my requirement, can you please let me know which programming language or IDE and Control should I use to develop this application.
PS: the image is made using mspaint and does not represent actual program.
As for language I would suggest either C# or VB.Net. They are both great languages and there is tons of support on the Internet for them. Just pick the one you are most comfortable with. As for a control that does what you are asking,
As for the control, it really depends on what platform you target. I have very little experience with WPF so can't help you with that. However, if you want to use WinForms, I would suggest the ListView control. It provides a lot of flexibility in terms of how you can use it. You will need to write the code to identify the rows and highlight them, but that shouldn't be too difficult once you understand how the ListView works.
Here are a couple tips if you do use ListView:
Set ListView.View to Details (this provides you with a grid-like control)
Set ListView.HideSelection to false
Set ListView.FullRowSelect to true
Set ListView.BackColor to Black
Set ListView.ForeColor to White
You can hide the column headers by setting ListView.HeaderStyle to None
If you want to support selecting multiple rows, you can set ListView.MultiSelect to true
To highlight row, set the ListViewItem.BackColor and ForeColor
The big let down of this control is that you don't have much control over the color of selected rows. There are techniques out there to control this, but it's not a simple property set. If you are interested, I think this question/answer might help.

VB in Access: Combo Box Values are not visible in form view but are visible through Debug.Print

Code in Form onLoad:
country_combo.RowSourceType = "Value List"
Code in a reset function:
Dim lListIndex As Long
With Me.country_combo
For lListIndex = .ListCount - 1 To 0 Step -1
.RemoveItem (lListIndex)
Next lListIndex<br/>
End With
Code to populate country combo:
*For n = 1 To numCountries*
*countryCombo.AddItem (countryRS.Fields("countryName"))*
*countryRS.MoveNext*
*Next n*
I'm having a problem that occurs AFTER the code to populate the country combobox runs. The values are there as I can run Debug.Print(countryCombo.Value) and it prints out the name of the selected country, but I can't see the values in the combobox at all. They're invisible, and as far as I know there is no visiblity property for specific items, unless I'm completely mistaken.
comboBoxError.png http://img110.imageshack.us/my.php?image=comboboxerror.png
I think you should probably use Access's GUI tools to do what you're looking for. In design mode, click on the field you are trying to populate, then click the "lookup" tab. You can then specify a table to populate the field with and your forms should automaticly update as well.
I've also seen what you describe here - as far as I can tell, it's a bug within Access (I was using 2007) that only occurs when you programatically mess with the contents of a combo box. It does not happen every time. The issue corrects itself if you highlight the text that is in the combo box.
I am experiencing a similar issue with Access 2003. Based on the selection of one combo box, the row source of a listbox is set to an SQL string Basically a SELECT DISTINCT [MyField_Selected] FROM MyTable. For some fields the values are visible in the list box and others it is not. The values are there however as I can access them via code. To make it more interesting it works fine in Access 2007.
Just found the resolution on another forum. Check the format property of the field(s) in question on the table. In my case, when Access 2007 created the table, it put an # format in there. I removed that and all works great!