473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to add additional values to a rowsource value

I have several combo boxes in a form as they link to more information (for example customerprojectmanager, when selected would not only allow you to select the project manager, but would also then drag a whole load of information into the background of the form, such as telephone number, email address, etc, etc).

All is fine so far, however, to add new project managers I originally had a button called admin tools, that would allow you to add new values for all the different combo boxes and again, that worked fine. However, according to the powers that be, thats confusing.

What I have been instructed to do is have a list such as this:

Name1
Name2
Name3
Add New Name

where obviously the first part can be picked up with

Expand|Select|Wrap|Line Numbers
  1. CustomerProjectManager.RowSource = "SELECT CustomerPM.CustomerProjectManager FROM CustomerPM WHERE CustomerPM.CustomerName = " & "CustomerName.Value"
however, How do I then add on "Add New Name" to this list?
I want to make it so when you select that it will then open the relevant form to allow the addition of the additional data.

And thats where I'm stuck!!

Help!

Thanks
Nov 2 '10 #1
15 7634
gnawoncents
214 100+
Add "Add New Name" to your table so it shows up at the bottom/top of your list (use a special character if you need). Then you can set up an if statement in the after update event of the combo box that would then open the form. Something like this:

Expand|Select|Wrap|Line Numbers
  1. If [customerprojectmanager] = "Add New Name" Then
  2.     DoCmd.OpenForm "YourFormHere"
  3.     Exit Sub
  4. End If 
That way whenever someone selects "Add New Name" it will open the form to well... add a new name. Otherwise it will execute whatever code you need for the manager selected.
Nov 2 '10 #2
NeoPa
32,556 Expert Mod 16PB
You need to add in an extra SELECT to your SQL using the UNION ALL clause. Use TOP 1 to ensure it only appears once :

Expand|Select|Wrap|Line Numbers
  1. With Me.CustomerProjectManager
  2.     .RowSource = "SELECT [CustomerProjectManager] " & _
  3.                  "FROM   [CustomerPM] " & _
  4.                  "WHERE  [CustomerName]=" & Me.CustomerName & _
  5.                  "UNION ALL " & _
  6.                  "SELECT TOP 1 'Add New Name' " & _
  7.                  "FROM   [CustomerPM]"
  8. End With
Nov 2 '10 #3
I tried the Union All code, and now I am getting an entirely blank combo box. Any ideas why?
Nov 3 '10 #4
NeoPa
32,556 Expert Mod 16PB
Not without seeing what you actually tried.

I have to assume that you have made no other changes too of course. Changing properties of your ComboBox control could muck things up, but I only know what you tell me.
Nov 4 '10 #5
The current code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerName_AfterUpdate()
  2.  
  3. If [CustomerName] = "Add New" Then
  4. Me.CustomerName = ""
  5. DoCmd.OpenForm "CustomerDetailsInputForm", acNormal, , , acFormAdd, acWindowNormal
  6. Exit Sub
  7. Else
  8. With Me.CustomerProjectManager
  9.     .RowSource = "SELECT [CustomerProjectManager] FROM [CustomerPM] WHERE [CustomerName] = " & Me.CustomerName & "UNION ALL SELECT TOP 1 'Add New' FROM CustomerPM"
  10. End With
  11.  
  12. End If
  13.  
  14. End Sub
  15.  
But unfortunately the combo box is still blank. Any ideas what I am doing wrong?

Thanks

James
Nov 10 '10 #6
NeoPa
32,556 Expert Mod 16PB
Ah. I think I see the problem now. You stupidly followed my instructions, which I'm ashamed to say, had at least two newbie errors in them.

Let me see if I can suggest some more reliable code for you :
Expand|Select|Wrap|Line Numbers
  1. With Me.CustomerProjectManager
  2.     .RowSource = "SELECT [CustomerProjectManager] " & _
  3.                  "FROM   [CustomerPM] " & _
  4.                  "WHERE  [CustomerName]='" & Me.CustomerName & "' " & _
  5.                  "UNION ALL " & _
  6.                  "SELECT TOP 1 'Add New Name' " & _
  7.                  "FROM   [CustomerPM]"
  8. End With
I hope you find this more reliable :-D
Nov 10 '10 #7
Perfect!! Thanks Very much.
Nov 10 '10 #8
NeoPa
32,556 Expert Mod 16PB
No worries. I'm just sorry I gave you such a bum lead in the first place. Such basic errors too.
Nov 10 '10 #9
Just realised one very small issue, the combo boxes are now no longer in alphabetical order when clicked on. How do I go about changing this? (Preferably leaving Add New at the bottom as it currently is with your code).

Ta!
Nov 11 '10 #10
NeoPa
32,556 Expert Mod 16PB
Unfortunately, it seems that when UNION is used in any form, it ignores any ordering except that after the whole dataset has been brought together (I even tried sorting the data by including the ORDER BY clause in a subquery - but even that had no effect).

It seems you have two choices then :-
  1. Leave it as it is, with the "Add New Name" entry last.
  2. Sort it, but have the "Add New Name" entry appear wherever it does alphabetically in the list.

There is a third choice I suppose, which is to add an alphabetically high character to the value so that it appears at the end after sorting. Unfortunately, I only found 'z' to work well in this context. Every other character I tried seemed to be treated as another version of characters lower down the order, and thus the entry was not found at the end.

Anyway, the code for sorting the results would be :
Expand|Select|Wrap|Line Numbers
  1. With Me.CustomerProjectManager
  2.     .RowSource = "SELECT   [CustomerProjectManager] " & _
  3.                  "FROM     [CustomerPM] " & _
  4.                  "WHERE    [CustomerName]='" & Me.CustomerName & "' " & _
  5.                  "UNION ALL " & _
  6.                  "SELECT TOP 1 'Add New Name' " & _
  7.                  "FROM     [CustomerPM] " & _
  8.                  "ORDER BY [CustomerProjectManager]"
  9. End With
Nov 11 '10 #11
NeoPa
32,556 Expert Mod 16PB
Actually, I suppose there might be a way. It's getting a little cute, but may be something you'd like. It involves putting the resultant data from the UNION query in as a subquery source of an outer query that sorts how we want it to, then displays only the textual result ([CustomerProjectManager]).

It's a bit more involved so I'd do this with an intermediate string variable (strSQL) which would need to be Dimmed first. The Dim line would be put at the start of your event procedure and would look like :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
The rest of the code would be something like :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT   0 AS [Order]" & _
  2.          "       , [CustomerProjectManager] " & _
  3.          "FROM     [CustomerPM] " & _
  4.          "WHERE    [CustomerName]='" & Me.CustomerName & "' " & _
  5.          "UNION ALL " & _
  6.          "SELECT TOP 1 " & _
  7.          "         1" & _
  8.          "       , 'Add New Name' " & _
  9.          "FROM     [CustomerPM]"
  10. With Me.CustomerProjectManager
  11.     .RowSource = "SELECT   [CustomerProjectManager] " & _
  12.                  "FROM     (" & strSQL & ") " & _
  13.                  "ORDER BY [Order]" & _
  14.                  "       , [CustomerProjectManager]"
  15. End With
Nov 11 '10 #12
Crikey, that looks quite involved! I'll try it out and let you know how I get on! Thanks
Nov 11 '10 #13
NeoPa
32,556 Expert Mod 16PB
James Bowyer:
Crikey, that looks quite involved!
Indeed James. That's why I felt it would be helpful to split away the string for the inner (sub) query. That string can be tested directly too of course. If you shove the resultant string value in the SQL view of a query you can see the results on screen.
Nov 12 '10 #14
It works! I had to add a few &'s (you did that just to check I was paying attention I'm sure) but other than that, perfect.

Now I just need to stare at it long enough to understand exactly how it works!
Nov 15 '10 #15
NeoPa
32,556 Expert Mod 16PB
I seem to be making a bunch of sloppy errors in this thread James. You caught them though, so that's good.

I've updated my post (#12 - with an edit comment) to show what it should have looked like ;-D
Nov 15 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Frank | last post by:
I have a large form, that has text boxes of numbers in rows and columns. I need to sum the values in the columns, and put the total at the bottom of the column. But I also need to sum the values in...
6
by: Sebi | last post by:
Hello all, I'm thinking about overwriting the ListItem, so it can contain x additional values (not only one). Has anybody ever tried this? Has someone got an example (C#)? Can DropDownList...
6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
2
by: John | last post by:
Hi I have a query as a rowsource to a list. Is it possible to add a single additional value to the row source to appear in the list in addition to what is coming from the query? Such as by...
1
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems...
34
by: bitsnbytes64 | last post by:
Hi, I've been having a similar issue to what is described in the "refresh a form" post with a ComboBox that is not being refreshed after adding a new value on a seperate form. The second form is...
2
by: sodkgb | last post by:
This is my first time posting to the forum and welcome assistance to fix this javascript: <script language="JavaScript"> <!-- function calculate(what) { what.answer.value =0; for (var...
1
by: mlgmlg | last post by:
Hello, I have a text box (on a Single Form) that I use to color code milestones based on a value. I use the “Conditional Formatting” tool to format four of the conditions, which works great! ...
16
by: bbatson | last post by:
Hello, Suppose I have combo box that is linked to the following SQL statement: "SELECT DISTINCT Column1 FROM Tbl1;" Suppose the results are 3 fields: Red, Green, Yellow What if I wanted to...
1
by: jay123987 | last post by:
Happy new year all - hope you had a great one! I am kind of stuck in regards to creating a particular function on a form. I currently have a form that allows the operator to insert details or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.