473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add a forms label to a query

489 Contributor
I have a query with fields defined from a table. I have a label on a form that I would like to add to this query, the forms name is params and the label name is match4. The labels caption can be changed on the form and I want that new caption to be in the query, can this be done. Any help would be appreciated.
Thanks again for your help.
Sep 12 '12 #1
11 5571
zmbd
5,501 Recognized Expert Moderator Expert
If you are talking about a query that you have built from the ribbon/menu then the only way is to use the qrydef collection.
Delete the old query and then rebuild and add it to the collection.
If you are talking about an sql string that is built in VBA and then used to open a record set, then just use the field caption in your code to build the string.

-z
Sep 12 '12 #2
CD Tom
489 Contributor
Thanks for the reply. I'm not sure how to add a label from a form to a query. I use the simple query design not the wizard when I usually design a query, although I do also use the sql string to build query's when things in the query need to change. I not sure how to code a form label in the sql string or when I just use the query design.
Sep 12 '12 #3
zmbd
5,501 Recognized Expert Moderator Expert
OK
In order not to go into the deep end we're going to have to understand what it is you're doing. Although it is not unusual to change a label caption (I use them for user feed back) it is unusual to use the control as I suspect you are doing; thus, please provide some detail as to what you're doing with the form and some example code with an explanation of any errors. (when you post code, select it with the mouse and click the <code/> button above next to the undo/redo.

-z
Sep 12 '12 #4
CD Tom
489 Contributor
Boy, let's see if I can explain this further. I really don't have any code to show you as the only thing the query does is export the data to a .txt file. But here's what I'm trying to accomplish. The Params form is used to set all the options for the program. In the params form the user can setup 5 events, this is where the label caption comes in, they can change the names of the events, this give the user a little more flexibility so instead of just a standard name they can name the event what ever they wish. The user then can export the data to the .txt file where a hand held computer will import that data. The data for the hand held computer comes from the single table but I want to have the event name also show up with that data. I probably could add some columns to the table and store the data in there but I would like to just and the caption fields to the query.
I hope this makes sense if not let me know and I'll try again.
Sep 13 '12 #5
Rabbit
12,516 Recognized Expert Moderator MVP
I don't understand either why you would want to include the label caption as a field in a query. Label captions don't change and are static unless you're using code/macros to change the caption value.
But if you use this in a query def, it will pull the label caption into the query.
Expand|Select|Wrap|Line Numbers
  1. SELECT PK, [Forms]![FormName]![LabelName].[Caption] AS LabelCaption
  2. FROM someTable
With that, if my label caption is Name: and I have 3 records in the table, my results would be:
Expand|Select|Wrap|Line Numbers
  1. 1 Name:
  2. 2 Name:
  3. 3 Name:
Sep 13 '12 #6
zmbd
5,501 Recognized Expert Moderator Expert
I think what we have here is a failure to communicate

:) (chuckle)
Take a look at the following picture.
I believe that you are referring to textbox controls and not labels as labels are not normally something a user can modify in form view.

So all of the "unbound" entries are controls for user interaction.

If we're talking about textbox controls then you have basically the same thing as Rabbit's just for values:
Expand|Select|Wrap|Line Numbers
  1. SELECT PK, 
  2.    [Forms]![FormName]![TextBoxName].[Value] 
  3.   AS TextBoxValue 
  4. FROM someTable 
-z
Attached Images
File Type: jpg byte_frm_example.jpg (114.1 KB, 2016 views)
Sep 13 '12 #7
CD Tom
489 Contributor
I understand the difference between the Textbox and the Label and I do use the Label. I know this is probably not the correct way to do this but because this is a new option in the program to extract this data out to a .txt file I'm trying to make it so I don't have to make big changes to the program. I tried the do the querydef but it doesn't work here's the code to build the query
Expand|Select|Wrap|Line Numbers
  1. (A cut and paste may not work on this )
  2. Set ExtractParm = db.QueryDefs("Extractparm")
  3. ExtractParm.SQL = 
  4.    "SELECT [forms]![Params]![Match4].[Caption]
  5.    as EventName, 
  6.    Params.Club_name, Params.RegionalName, 
  7.    Params.WildbunchStages, Params.Wildbunchdate,
  8.    Params.RankPointsWB, Params.TotalTimeWB,
  9.    Params.RankbyClassWB, Params.WBShotPoint, 
  10.    Params.MisseTime, Params.PenaltiesTime,    
  11.    Params.BonusstageWB1, Params.BonusstageWB2," & _
  12.    "Params.BonusstageWB3, Params.BonusstageWB4, 
  13.    Params.BonusstageWB5, Params.BonusstageWB6,    
  14.    Params.BonusstageWB7, Params.BonusstageWB8, 
  15.    Params.BonusstageWB9, Params.BonusstageWB10, 
  16.    Params.BonusstageWB11, Params.BonusstageWB12," & _
  17.    "Params.MatchDQTotal, Params.StageDQTotal,
  18.    Params.DNFTotal, Params.StageDQTargets,
  19.    Params.TargetPlus 
  20. FROM Params"
When I run the query it ask for a value to enter in the forms field.
Sep 13 '12 #8
zmbd
5,501 Recognized Expert Moderator Expert
CD Tom,
Hopefully you got the tag line... it's a play on Cool Hand Luke and the chuckle

I really was hoping that you ment text boxes and not labels. This would have been a very straight forward way of building a query.

By using labels for a job that text boxes are designed to do, you make your coding that much more difficult and the enduser will truely dislike haveing a dozen prompts showup (and I'm guessing here...) You won't beable to intercept the responses to build the query as you want because the engine is trying to get the values to compare against data... not values to build fields.

By using the textboxes, your user opens the form, enters the text in the boxes, and the vba builds the query for them when what ever command button is pressed. You can take the values and use them in a "where" clause or use them to build the query structure.

The effort (IMHO) to try and do what I think you are attempting will be somewhat the same, actually I think more difficult, as redoing what you've already done.

-z
Sep 14 '12 #9
CD Tom
489 Contributor
I guess I'll try and change those labels to textboxs and give that a try. Thanks for you advice and help.
Sep 14 '12 #10
CD Tom
489 Contributor
Ok, I changed those labels to textboxs and change the querydefs to .[value] but I get the same thing. I think maybe I need to come up with some other way to get this done.
Thanks for all your help.
Sep 14 '12 #11
zmbd
5,501 Recognized Expert Moderator Expert
CD Tom,
Don't give up just yet.
Let's look at the two textboxes Params.Club_name, Params.RegionalName for ease let's call them txtClubname and txtRegionalName.

When the user enters the information into these two boxes so that [textclubname]=userclubname and [txtRegionalName]=userregionalname let's determine what you want to do...

Somewhere we have to have a table to work with...

Now this table either has to have fields in it named "userclubname" and "userregionalname" or you're trying to gather records where the information in the table field meets that requirement.

Case one:
tbl_example1
[PK] autonumber - primary key
[userclubname] - text(50) required, no duplicate
[userregionalname] - text(50) required, no duplicate

Expand|Select|Wrap|Line Numbers
  1. 'This code is written from memory, it has not
  2. 'been checked for typos against the complier
  3. 'sorry, not at a PC with Access or VB installed
  4. '
  5. Dim zdb As DAO.Database, zqrydef As DAO.QueryDef
  6. Dim zsql as string
  7. Dim zuserinput1 AS string, zuserinput2 AS string 
  8. '
  9. 'setup for the database
  10. Set zdb = CurrentDB
  11. '
  12. 'check to see if the query exsists
  13. 'qry_example
  14. 'and if so then remove it from the collection
  15. For each zqrydef in zdb.querydefs
  16.    '
  17.    if lcase(zqrydef.name)="qry_example" then
  18.       zqrydef.delete "qry_example"
  19.    end if
  20. next zqrydef
  21. '
  22. 'now build the query for the selected fields:
  23. zuserinput1 = me.txtclubname.value
  24. 'example user entered userclubname
  25. '
  26. zuserinput2 = me.txtregionalname.value
  27. 'example user entered userregionalname
  28. '
  29. 'very simple select query, no wheres no whys
  30. 'just get the two fields from the table and
  31. 'show all records within the table tbl_example1
  32. '
  33. 'NOTE the space appended in string!
  34. zsql = "Select " & zuserinput1 & "," & _
  35.        "zuserinput2 & " " & _
  36.        "FROM tbl_example1;"
  37. '
  38. 'Using our example this string should evaluate to:
  39. 'Select userclubname,
  40. '   userregionalname
  41. 'From tbl_example1;
  42.  
  43. 'create the query (see why we searched first?)
  44. 'if you try to create a query with the same name
  45. 'access will have a tempertantrum
  46. '
  47. Set zqrydef = zdb.CreateQueryDef("qry_example", zsql)
  48. '
  49. 'That's it... you can add code to open the query
  50. 'the user can open the query from the navigation pane
  51. 'you can use the query in the transfersheet method, etc..
  52. '
  53. 'Clean UP - - Always a cleanup step
  54. zdb.close
  55. set zdb = nothing
  56. set zqrydef = nothing
  57. end sub
Case two:
tbl_example2
[PK] autonumber - primary key
[clubnames] - text(50) required, no duplicate
[regionalnames] - text(50) required, no duplicate

We're going to repeat some of the above code:
Expand|Select|Wrap|Line Numbers
  1. 'This code is written from memory, it has not
  2. 'been checked for typos against the complier
  3. 'sorry, not at a PC with Access or VB installed
  4. '
  5. Dim zdb As DAO.Database, 
  6. Dim zrs As DAO.recordset '<<<notice
  7. Dim zsql as string
  8. Dim zuserinput1 AS string, zuserinput2 AS string 
  9. '
  10. 'setup for the database
  11. Set zdb = CurrentDB
  12. '
  13. 'now build the query for the selected fields:
  14. zuserinput1 = me.txtclubname.value
  15. 'example user entered userclubname
  16. '
  17. zuserinput2 = me.txtregionalname.value
  18. 'example user entered userregionalname
  19. '
  20. 'very simple select query, no wheres no whys
  21. 'just get the two fields from the table and
  22. 'show all records within the table tbl_example2
  23. '
  24. 'NOTE the space appended in string!
  25. zsql = "Select * " & zuserinput1 & "," & _
  26.        "FROM tbl_example2 " & _
  27.        "WHERE(([clubnames] = '"  & zuserinput1 & "')" & _
  28.        "AND ([regionalnames] = '" & zuserinput2 & "'));"
  29. 'Using our example this string should evaluate to:
  30. 'Select *
  31. 'From tbl_example2
  32. 'WHERE (([clubnames] = 'userclubname') AND
  33. '(regionalnames] = 'userregionalname'));
  34. '
  35. 'Now lets open a record set based on the sql string:
  36. set zrs = zbd.openrecordset(zsql,dbOpenDynaset)
  37. '
  38. 'That's it... you can add code to pull information out
  39. 'of the record set for display on the forms or whatever
  40. 'you desire (well... almost)
  41. '
  42. 'Clean UP - - Always a cleanup step
  43. zdb.close
  44. set zdb = nothing
  45. set zrs = nothing
  46. end sub
As I said, I pulled all of the code for the two blocks out of my head... amazing how lazy I've gotten with the vba editor window... so there might be a syntax error.
-
z
Sep 14 '12 #12

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

Similar topics

3
2267
by: Joshua Russell | last post by:
Hi, Both the methods below open up a windows form called MasterForm. However, one works better than the other. Method 1 opens the form correctly but I don't have any reference to the instance of...
0
1536
by: Adonai | last post by:
Hi, I'm having an issue with this code that I'm writing, I'm curious as to why the windows form label (lblTime) isn't getting updated with the new variable when I use the timer. I made a blank form...
3
32475
by: jhs | last post by:
Hello, I developping a .NET windows form application an need some help to create an array of System.Windows.Forms.Label in order to be able to manage all of them using index. I'm trying to do...
7
2454
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
31
7150
by: jcrouse | last post by:
Is there a quick and easy way to change the color of a label controls border from the default black to white? Thank you, John
3
2395
by: Neil Wallace | last post by:
Hi, This is an odd one. I've been struggling to get "double click" to work well for my controls. The same event handler works perfectly for buttons, but not for labels. Can anyone tell me...
3
2357
by: Geraldine Hobley | last post by:
Hello, In my project I am inheriting several forms. However when I inherit from a form and add additional subroutines and methods to my inherited form I get all sorts of problems. e.g. I sometimes...
2
3177
by: deekay | last post by:
Im trying to update a database where our users have been entering/editing all data using queries to now use forms instead. The first step that I want to take is to convert the queries to forms in...
0
957
by: nave11 | last post by:
How to add Context menu to a class derived from System.Windows.Forms.Label ? This label will be dragged on a designer surface on runtime. For example, to add a context menu with "Copy" and "Paste"...
11
11466
by: Peter Larsen [] | last post by:
Hi, I have two questions related to the Label control. How do i prevent the label from expand the canvas into 2 lines (word wrap) ?? At the moment i set AutoSize to false (to prevent the word...
0
7271
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,...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.