473,396 Members | 1,784 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,396 software developers and data experts.

filling fields with query results

Hello,

Basically, I'm running a query on a form's activation, and I'd like to
have the results of the query be placed into other fields on the same
form automatically.

Does anybody know how this can be done?

I've tried setting the default value of the text fields on the form to
be equal to [NameOfQuery]![FieldOfQuery] using Access' expression
builder. This doesn't seem to work, always leaving the text fields
blank, even though the query results are accurate.

Any ideas?

Thanks,

Brian

Nov 13 '05 #1
6 17121
Brian wrote:
Hello,

Basically, I'm running a query on a form's activation, and I'd like to
have the results of the query be placed into other fields on the same
form automatically.

Does anybody know how this can be done?

I've tried setting the default value of the text fields on the form to
be equal to [NameOfQuery]![FieldOfQuery] using Access' expression
builder. This doesn't seem to work, always leaving the text fields
blank, even though the query results are accurate.

Any ideas?

Thanks,

Brian

Open the text box's property sheet, click on the Data tab, select
Control Source, and from the dropdown in that row select the field you
want filled in.

If that is not what you mean, you may want to open the query as a
recordset (I'll assume it is one record) and update the fields from the set.

Dim rst As Recordset
set rst = Currentdb.Openrecordset("QueryName",dbopensnapshot )
Me.FieldNameToFill = rst!TheFieldData
rst.close
set rst = Nothing

Nov 13 '05 #2
Yes. That's right. This query results in one record.

I tried your code as shown:

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("PO Sub Total Query",
dbOpenSnapshot)
Me.SubTotal = rst!SubTotal
rst.Close
Set rst = Nothing
where the name of my query is "PO Sub Total Query", the field I'm
trying to fill automatically is "SubTotal" and the field generated by
the query (which is an expression that sums up all PurchasePrice fields
of each record) is "SubTotal".

I get this error:

"Too few parameters. Expected 1."

Does anybody know what parameter is required? Sorry about all this. I
know Access pretty well minus the VB stuff.

Thanks!

Brian

Nov 13 '05 #3
"Brian" <bc****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Yes. That's right. This query results in one record.

I tried your code as shown:

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("PO Sub Total Query",
dbOpenSnapshot)
Me.SubTotal = rst!SubTotal
rst.Close
Set rst = Nothing
where the name of my query is "PO Sub Total Query", the field I'm
trying to fill automatically is "SubTotal" and the field generated by
the query (which is an expression that sums up all PurchasePrice fields
of each record) is "SubTotal".

I get this error:

"Too few parameters. Expected 1."

Does anybody know what parameter is required? Sorry about all this. I
know Access pretty well minus the VB stuff.

Thanks!

Brian


Just guessing, but you might try adding the following stmt
rst.MoveFirst
before the stmt: Me.SubTotal = rst!SubTotal

I haven't read this thread from the beginning,
but which form event are you using for this code?
Fred Zuckerman
Nov 13 '05 #4
Brian wrote:
Yes. That's right. This query results in one record.

I tried your code as shown:

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("PO Sub Total Query",
dbOpenSnapshot)
Me.SubTotal = rst!SubTotal
rst.Close
Set rst = Nothing
where the name of my query is "PO Sub Total Query", the field I'm
trying to fill automatically is "SubTotal" and the field generated by
the query (which is an expression that sums up all PurchasePrice fields
of each record) is "SubTotal".

I get this error:

"Too few parameters. Expected 1."

Does anybody know what parameter is required? Sorry about all this. I
know Access pretty well minus the VB stuff.

Thanks!

Brian


I would open up "PO Sub Total Query". Look for anything in the Criteria
row(s) that has a parameter...ex Forms!Mainform!Name...where the
criteria expects the name from a form...perhaps a date range...who
knows. If not there, it may be in another query "PO Sub Total Query" uses.

Maybe you can make the query more generic. Perhaps you need to look at
Parameters in on-line help for more detail.

Nov 13 '05 #5
Hello,

Damn you guys are good. Got it right on the button. In my query, one
criteria field contains [Forms]![Purchase Orders]![PurchaseOrderID],
which will isolate all assets in my database that contain
PurchaseOrderID's identical to the currently opened Purchase Order
form (i.e. to find the assets of the current PO). Currently, this is
the code that I have:
Private Sub RefreshPOForm_Click()
On Error GoTo Err_RefreshPOForm_Click

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("PO Sub Total Query",
dbOpenSnapshot)
Me.SubTotal = rst!SubTotal
rst.Close
Set rst = Nothing

Me.Refresh

Exit_RefreshPOForm_Click:
Exit Sub

Err_RefreshPOForm_Click:
MsgBox Err.Description
Resume Exit_RefreshPOForm_Click
End Sub
Basically I have a command button on the PO form that is dedicated to
refreshing the form (hence the Me.Refresh). I'd also like to update
all the fields on the form every time this button is pressed, hence me
wanting to auto fill fields based on the results of a query

I've only done Access for a few weeks now, and I pretty much
understand what is going on (in terms of ms access wizards...lol),
minus the VB coding aspect. Would you guys be able to help me out?

How would I go about setting up the query and its criteria field to
only obtain assets with a puchaseOrderID identical to the open Purchase
Order form's PurchaseOrderID, and be able to get the above code to
work?

Also, in terms of VB coding, would scope be an issue? Can the function
"Openrecordset" be able to access the query? Are queries global
objects?

Thanks again guys!

Brian

Nov 13 '05 #6
Brian wrote:
Hello,

Damn you guys are good. Got it right on the button. In my query, one
criteria field contains [Forms]![Purchase Orders]![PurchaseOrderID],
which will isolate all assets in my database that contain
PurchaseOrderID's identical to the currently opened Purchase Order
form (i.e. to find the assets of the current PO). Currently, this is
the code that I have:
Private Sub RefreshPOForm_Click()
On Error GoTo Err_RefreshPOForm_Click

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("PO Sub Total Query",
dbOpenSnapshot)
Me.SubTotal = rst!SubTotal
rst.Close
Set rst = Nothing

Me.Refresh

Exit_RefreshPOForm_Click:
Exit Sub

Err_RefreshPOForm_Click:
MsgBox Err.Description
Resume Exit_RefreshPOForm_Click
End Sub
Basically I have a command button on the PO form that is dedicated to
refreshing the form (hence the Me.Refresh). I'd also like to update
all the fields on the form every time this button is pressed, hence me
wanting to auto fill fields based on the results of a query

I've only done Access for a few weeks now, and I pretty much
understand what is going on (in terms of ms access wizards...lol),
minus the VB coding aspect. Would you guys be able to help me out?

How would I go about setting up the query and its criteria field to
only obtain assets with a puchaseOrderID identical to the open Purchase
Order form's PurchaseOrderID, and be able to get the above code to
work?

Also, in terms of VB coding, would scope be an issue? Can the function
"Openrecordset" be able to access the query? Are queries global
objects?

Thanks again guys!

Brian

Brian, oftentimes it is best to "select" the whole recordset ...with
some judicious filter conditions. For example, I may want to select all
records that are active, never those that are flagged inactive. So I
might have a query that selects the records and the required fields for
the query only for active records...but leave the filtering of records
for later...where I might want to filter records based on a name or a
date range or a department, etc.

So your "PO Sub Total Query" might do all of the summing and grouping.
But the filtering can take place later. For example, you could open a
form like this
Docmd.Openform "FormName",,,"PONum = 123"
and this will filter the recordset and only display the record for
PONumber = 123

You can also filter the recordset with anothe SQL. ExL
Dim rst As Recordset
Dim strSQL As String
strSQL = "Select * From [PO Sub Total Query] Where PONum = 123"
set rst = currentdb.openrecordset(strSQL,dbopensnapshot)
...do what ever you want for all records where PONum is 123
rst.close
set rst = Nothing

Queries are not, per-se, global objects...they are database
objects...just as tables, forms, reports, macros, and modules are. They
are more similar to tables. You can have a table called Employees. If
you do, you can't have a query called Employees...it must have a
different name.

This is a side note...I would HIGHLY recommend you not use spaces in
table, query, form, and report names. What many programmers do is
capitalize the first character of a word. For Example, PO Sub Total
Query would be called POSubTotalQuery. Having spaces in names works,
but it adds additional responsibilities to the programmer. You need to
put [] brackets around the names which adds more typing to your code.

The times I may use a space in a query is when I wish to present column
headings in a listbox or combo box. Let's say the field is CustNum, but
in the row heading column of a list box or combo box I want to see
Customer #. In my rowsource for that combo/list box I might enter
Customer # : CustNum

This is just a recommendation...you can follow or ignore. Over time, I
think you'll find it good advice.

Also, some people add tbl in front of table names, qry in front of query
names, frm in front of form names. Again, it's fine for some but I find
it a PITA. When I work in the database window, I want to see things in
alphabetical order without 3 extra chaacters in front of the
name...those 3 characters just get in the way...IMHO.

However, I strongly recommend you used int in front of integers, str in
front of strings, lng in front of longs, dat in front of dates...etc
inside your code. Ex:
Dim strSQL As String
Dim intFlag as Integer
The reason for this is that you always know what type of variable you
are working with.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
5
by: -elniniol999- | last post by:
I am DESPERATE for a quick reply.. have exhausted my options... i have a table called order details which contains: order id product id product name quantity unit price sale price
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
1
by: gaucho | last post by:
Hi all, I'm experiencing some problems when filling in bookmarks in word. With my first query (single row returned), no problem at all. Yet, with my new query (which now return 2 rows), i get...
3
by: Russ | last post by:
I have a drop down list on a web page that I pre-fill onload from a SQL table using a SqlDataReader. The line of code that fills the drop down looks like this while(myReader.Read()) {...
3
by: crjunk | last post by:
I have a 3 table in my DataSet that I'm filling with data. After I've filled these 3 tables, I'm then trying to run a query that will fill a 4th table in the DataSet with data from the three...
2
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
5
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a form valiadation for now i can tell people that they made a mistake filling out the form but my script to tell them which fields they missed isn't working. I think what my...
8
by: olivero | last post by:
Hi group, Is there an easy way to make a form create the same set of fields dynamically, once for each record returned by a query? I have a query that's being called from a form and the results...
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:
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.