I have a listbox with values. Now I need to sort that listbox, BUT NOT BY ALPHABETICALLY.
You see the values in a listbox are from a table.
------------------------
| name | order | size |
========================
value1 4
value2 3
value3 1
value4 2
I hope I made myself clear. So the list box has the items "value1, value2, value3, value4". I want it to sort by the order declared in the table. The list box has nothing to do with the table and is not "data bound" to it. The values come from somewhere else.
Tech used: VBA, Access 2007
It sounds like the Row Source Type for your list box is "Value List". Since the values come from a table, change Row Source Type to "Table/Query", then use a query for Row Source:
SELECT [name], [order]
FROM YourTable
ORDER BY [order];
You don't have to display [order] in the list box ... set its column width to zero.
Your list box control does not have to be bound to a data source field for this approach to work. If the list box is unbound, the user selection will not be stored, but the list box will still allow users to make selections from the list box.
Notice I enclosed [name] and [order] in square brackets because both are reserved words. See Problem names and reserved words in Access
Dim First As Integer
Dim Last As Integer
Dim a As Integer
Dim b As Integer
Dim Temp As String
Dim test() As String
First = LBound(test)
Last = UBound(test)
For a = First To Last - 1
For b = a + 1 To Last
If test(a) > test(b) Then
Temp = test(b)
test(b) = test(a)
test(a) = Temp
End If
Next b
Next a
For a = 1 To UBound(test)
lst_email.AddItem test(a) 'lst_email is a listbox
Next
Related
I am working on this database in Access.
Here is how the table is:
in this table the conversion fromid and toid has to match that's why I have it structured like this. That is if I select MT, then I can only convert to another MT or ST. if I select ST in the base column then I can convert to another ST or MT.
I created this form below. it has 2 combo box. the second combo box is limited based on the selection from the first combo box. That is If I select MT in the first combo, then the only options available in the second combo box is MT or ST. If I select KG then combo box 2 is limited to another KG or LB.
but the problem is that, If I select one from the combo box 1, the matching pairs are not showing. that is if I select MT it doesn't limit to MT or ST, but show two other values like L and GLL.
I have attached a copy of the database.
Here is the link to the database I am testing with
Below is the snapshots of the code for each:
Your ID System with fromID to TOID does not seem to make any sense to me. First of all the pair is always the same. What is the intended benefit of this?
Alternative, just use the Text columns of Base and To instead and ajust properties of combo0:
Row Source: SELECT DISTINCT base FROM tblconversions;
Bound Column: 1
And the After Update method
Private Sub Combo0_afterUpdate()
Dim stoSource As String
stoSource = "SELECT DISTINCT tblconversions.To FROM tblconversions WHERE tblconversions.to = '" & Me.Combo0.Value & "'"
Me.Combo2.RowSource = stoSource
Me.Combo2.Requery
End Sub
I know this has probably been asked thousands of times before, but I seem to be having trouble finding an existing question that addresses this question in a wy I can understand.
My question in how can I access a value located in the last record of a table? For example, if this is my table:
personID personName personNumber
1 Sally PR32
2 Emily PR33
3 Joseph PR34
I want to access the value "PR34" (and then be able to manipulate it if possible). I know how to find the last record with Dmax, but I'm struggling to figure out how to find a value within it. I'm ultimately trying to take the value, parse the string down to just the numbers, and increment it by one (and then append the letters back on).
You can do this in a query as well:
UPDATE T
SET T.personNumber = "PR" & CLng(Replace([personNumber], "PR", "")) + 1
WHERE (((T.personID)=(SELECT Max(personID) FROM T)));
Where T is the name of your Table.
If you're in the DLookUp/DMax workflow, you can just use the following:
This gives you the ID, you already figured this out:
DMax("PersonID", "MyTable")
Then lookup the associated personNumber with that ID:
DLookUp("personNumber", "MyTable", "personID = " & DMax("PersonID", "MyTable"))
But if you want to manipulate it (or just be more efficient), using a recordset is the way to go:
'Create a recordset
Dim rs As Recordset
'Query the last value, get the personNumber
Set rs = CurrentDb.OpenRecordset("SELECT TOP 1 personNumber FROM MyTable ORDER BY personID DESC")
'Open it up for editing
rs.Edit
'Increment the number by 1
rs.Fields("personNumber").Value = Left(rs.Fields("personNumber").Value, 2) & CInt(Mid(rs.Fields("personNumber").Value, 3, Len(rs.Fields("personNumber").Value) - 2)) + 1
'Update the recordset
rs.Update
I have a table named Persons such as :
ID Name
1 John
2 Jerry
3 Jack
I have set up a form named Form1 with a Combo Box named Combo1 .
Combo1 has 3 values-
1
2
3
All
Then I set up the following query:
Select * From Persons WHERE Persons.ID = (Forms![Form1]![Combo1]);
The query works fine when 1,2 or 3 are selected in the Combo Box but I can't figure out how to make it work to select all the records when the user selects 'All'.
I don't see why VBA is required. Seems to me you can get what you need with only a query ...
Select *
From Persons
WHERE
Persons.ID = Forms![Form1]![Combo1]
OR Forms![Form1]![Combo1] = 'All';
When All is selected in the combo box, the condition Forms![Form1]![Combo1] = 'All' will be True for all rows in the table. So none are excluded from the query's result set.
When anything other than All is selected in the combo box, the query will return only rows whose ID value matches the combo box value.
Not sure what your Combo1 looks like now?
Are you asking to show all entries with ID > 1 or all entries > than the ID selected in Combo1 - and do you still have the ALL option in Combo 1?
If you're just asking your last question:
How can I make it work so that the user can view All Names for ID's
greater than 1?
Then you can probably use
Select * From Persons WHERE Persons.ID > 1
AND (Persons.Name = Forms![Form1]![Combo2] OR Forms![Form1]![Combo2] = 'All');
But right now I think you have everyone confused with what you want. Hope I guessed right and put you on the correct path
I'm new to Access and SQL so I'm having a bit of trouble with this undoubtedly simple issue.
I'm using VBA and SQL to update a table based on a few listbox selections.
One of the fields I'm updating is an "instance" field which tracks the instance number of each ID. For example, the first time ID 5 was added to the table its instance number would be 1. The second time it was added to the table its instance would be 2. So it would look like this:
ID | Date | InstanceNUM |
005 | 11/22/2013 | 1 |
005 | 11/23/2013 | 2 |
and so on. It's likely that other records would/will separate the two records in the example above.
After doing some research, it seems like DLast is the best function for this job since the last instance of the particular ID will also contain the instance number in field three. However, when I use this syntax:
Dim i As Long
Dim IDnum As Long
i = DLast("[InstanceNum]", "tbl_Data", [ID] = IDnum)
'where [InstanceNum] is the field name for the value I want to return, _
'"tbl_Data" is the table name where the value I want to return exists, _
'and [ID] is the field name where the ID exists.
I get the Run-time error '94': Invalid use of Null. My understanding of this error is that DLast couldn't find a value based on this criteria (and it can't store that result in a Long data type variable), but I know that the record exists (I've checked the table for the value and I've checked the variable for the value).
EDIT
The [ID] field is a text data type. I set the IDnum variable this way:
Dim IDnum As String
Set ctlList = Me!List7
For i = 0 To ctlList.ListCount - 1
If ctlList.Selected(i) = True Then
IDnum = ctlList.Column(0, i)
Exit For
End If
Next I
The above code loops through a listbox in a form I have and sets the IDnum variable as the ID number in the first column.
Per HansUp's suggestion, I have updated my code to this:
i = DLast("InstanceNum", "tbl_Data", "[ID] =" & IDnum)
But now I get the Run-time error '3464': Data type mismatch in criteria expression.
I'm at a loss for why this is happening. Perhaps there's an easy answer to this, but I haven't been able to find one... And maybe there's an easier way to do this? Does anyone know why this error is happening?
Since [ID] is text type, add quotes around the value of IDnum for DLast.
i = DLast("InstanceNum", "tbl_Data", "[ID] = '" & IDnum & "'")
However if the stored [ID] values include leading zeros, you must Format IDnum to include those.
i = DLast("InstanceNum", "tbl_Data", "[ID] = '" & Format(IDnum, "000") & "'")
I am currently using a continuous form to a show a set of data that results in the following format (each line being one record):
1/04/13 Person1
31/03/13 Person1
30/03/13 Person1
29/03/13 Person2
28/03/13 Person1
There are 100s of these records and I would like to condense it to the following:
30/03/13 - 1/04/13 Person1
29/03/13 Person2
28/03/13 Person1
The data is from a query, so can be manipulated at the SQL level before it even gets to the form stage. I am also comfortable using macros or VBA to manipulate the form. However, I still haven't been able to find a suitable way of doing this.
I realise that this is typical behaviour for a report, but since I eventually want this to be embedded in another form as a subform, I can't use reports (can't create a subreport in a form unless I am missing something).
Any help or advice is much appreciated.
Good description of the problem. Try building your source query like this (assuming the fields are called MyDate and MyPerson and the source table is MyTable):
Select Person, min(MyDate) as StartDate, max(MyDate) as EndDate
from MyTable
group by Person
This gives you 3 fields that you can drop into your form.
Edit
Now that we have a more complete picture, the task just got more complicated, but you still have options assuming the 'events' between people don't overlap (if they do, then I can't think of how to create eventIDs afterward). In the absence of an EventID, you'll have to create one:
First, you need to move your data to a new table so that you can reorder it and add a blank field for the event ID you'll be adding in step 2.
1a. Copy MyTable into a new table. Add an extra field called EventID.
1b. To populate this table, first Delete * from TmpTbl
1c. Then something like:
Insert into TmpTbl.* from MyTable order by MyDate,MyPerson
This code will look through your temp table (TmpTbl as i call it) and add an event id.
Sub AddEventIDs()
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("TmpTbl")
Dim LastPerson As String
Dim EventID As Integer
docmd.setwarnings false
While rst.EOF = False
If LastPerson <> rst.Fields("MyPerson") Then EventID = EventID + 1
LastPerson = rst.Fields("MyPerson")
rst.Edit
rst.Fields("EventID") = EventID
rst.Update
rst.MoveNext
Wend
docmd.setwarnings true
End Sub
On the On Load event of your form, add in the 2 queries and the sub routine from #1 and #2.
Your final query will be:
Select MyPerson, EventID, min(MyDate) as StartDate, max(MyDate) as EndDate
from MyTable
group by MyPerson,EventID