MS Access Forum / Forms / March 2008
A little trouble with a trivially simple form
|
|
Thread rating:  |
Ted - 12 Mar 2008 16:59 GMT I am afraid I am a little rusty WRT MS Access (the last version I used was MS Access 2000, and THAT was only to fix to referential integrity problems and create additional queries for an Access app that someone else had created and couldn't fix). I am presently working against MS Access 2003.
I am fixing a form on a little DB. The form uses a table, and supports editing it, not just viewing it, and there are several controls at the bottom for viewing summary data. For example, there is a column in the table called value, and one of the controls with summary data contains the sum of the values in that column. Simple enough. But now I have been asked to add an equivalent set of summary controls, and to divide the data into items sold and not sold. So the label that goes with the control having the sum of the value column will be replaced by the sum of the value column values on rows where another column has a value <> "sold", and there'd be a second control below it for the sum of values in the value column for rows where that other column has the value "sold"
I tried doing this with a pair of queries, as in: SELECT SUM(value) FROM engines WHERE col1<>"sold";
Now, the SQL is trivially simple, and when I open the queries, they give me the correct data. However, when I tried to use the query as a data source for a text box, and open the form, the values from the query are not displayed and instead I get a silly string '#NAME' I do not understand why this form can't access the data from a pair of queries that work flawlessly independantly, or even give the control the SQL statements required to get the right result.
I did try a subform, but it insists on putting the data in a grid, with controls to navigate through a series of records, and obviously with the summary data query there is only one record.
Using an expression like "=SUM(value)" seems to work, in that the total for all records appears, but I have not figured out how to apply the where clause to that kind of expression. Just giving the SQL as the data source gives the same silly string '#NAME' when the form is opened. Why isn't it obvious how to set up a pair of controls that use the same SUM(value) expression, and apply a filter colX="sold" to one and colX<>"sold" to the other?
I guess I should add that one additional requirement, apart from the partition of the sums according to whether or not the item is sold, is that when an item is sold (and the DB updated accodingly), the sums displayed ought to be updated immediately. Again, when I use the subform, despite it being really ugly for this purpose, and change an item to indicate it has been sold, the controls containing data for that item update immediately (including one that shows the profit from the sale), but the summary data is not updated until the next time I open the form.
There are reasons I prefer to work with MS SQL Server, PostgreSQL or MySQL, with a C++ or Java GUI. :-( This is turning into a reminder why I am not fond of programming MS Office products. :-( Maybe, admittedly, this is a catch 22 situation where I find programming MS Office products painful because I don't do it much, and I don't do it much because it has never been a really rewarding experience for me.
This really ought to be a trivial fix (and would take me all of 10 minutes if I were doing it using JDBC and a product like MS SQL Server), but I am getting really frustrated and the docs and books I have at hand are not much help.
Any tips that will help me get this done quickly, would be greatly appreciated.
thanks
Ted
Klatuu - 12 Mar 2008 19:18 GMT Good plan, come to an Access newsgroup for help and bash the product.
But, to answer your question, your problem starts here: However, when I tried to use the query as a data source for a text box
Text boxes do not have a data source. They have a control source. That is, you bind a field in the form's record source to a control so it can be displayed and modified. Of course, there are other uses for a control source, but that is beyond the current scope.
If you look at the other code that works for this, you will likely find one of two methods being used.
1. Modify parameters in a form's record source query and requery the form.
2. Build a filter string for that defines criteria for filtering. It is exactly like an SQL WHERE clause without the word where. Then filter the form using:
With Me .Filter = strFilter .FilterOn = True End With
I usually use method two. Here is an example:
Private Function SetFilters() As String Dim strFilter As String 'Used by Form Filtering procedures On Error GoTo SetFilters_Error
With Me 'Build The Filter String If .cboPriority <> "(All)" Then strFilter = "[InitPriority] = " & .cboPriority End If If .cboOrigDate <> "(All)" Then strFilter = AddAnd(strFilter) strFilter = strFilter & "Format([OrigReleaseTarget], ""yyyy-mm"") = """ & _ .cboOrigDate & """" End If If .cboCurrDate <> "(All)" Then strFilter = AddAnd(strFilter) strFilter = strFilter & "Format([CurrentReleaseTarget], ""yyyy-mm"") = """ & _ .cboCurrDate & """" End If If .cboInitStatus <> 0 Then strFilter = AddAnd(strFilter) strFilter = strFilter & "[InitStatus] = " & .cboInitStatus End If If .cboInitType <> 0 Then strFilter = AddAnd(strFilter) strFilter = strFilter & "[InitType] = " & .cboInitType End If If Not IsNull(.txtDescrSearch) Then strFilter = AddAnd(strFilter) strFilter = strFilter & "[InitShortDescr] Like ""*" & Me.txtDescrSearch & "*""" End If If Len(strFilter) > 0 Then .subInitiative.Form.Filter = strFilter .subInitiative.Form.FilterOn = True .subInitiative.Form.Requery Else .subInitiative.Form.FilterOn = False End If strFilter = AddAnd(strFilter) strFilter = strFilter & "[CcID] = " & Me.cboCenter End With 'Me
SetFilters = strFilter
***********
Private Function AddAnd(strFilterString) As String On Error GoTo AddAnd_Error
If Len(strFilterString) > 0 Then AddAnd = strFilterString & " AND " Else AddAnd = strFilterString End If
AddAnd_Exit:
Exit Function On Error GoTo 0
AddAnd_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & _ ") in procedure AddAnd of VBA Document Form_frmStartForm" GoTo AddAnd_Exit
End Function
**************
If Len(strFilter) > 0 Then .subInitiative.Form.Filter = strFilter .subInitiative.Form.FilterOn = True .subInitiative.Form.Requery Else .subInitiative.Form.FilterOn = False End If
 Signature Dave Hargis, Microsoft Access MVP
> I am afraid I am a little rusty WRT MS Access (the last version I used > was MS Access 2000, and THAT was only to fix to referential integrity [quoted text clipped - 66 lines] > > Ted Ted - 12 Mar 2008 19:49 GMT > Good plan, come to an Access newsgroup for help and bash the product. > [quoted text clipped - 187 lines] > > - Show quoted text - Thanks.
1) It wasn't my intention to bash the product; just to express my frustration with it.
2) The form is based on using all records in one table. The code, in the control, that works is precisely "=Sum([Value])": no filter or anything.
3) Is it not possible to apply a filter at a granularity finer than that of the form? Can I apply a filter to individual controls? We don't want to change how the main form works, or change the number of records it displays, and we do want to show sums, using a different filter, at the same time, one above the other. The raw data for the two sums of interest are in the same column in the same table, but on mutually exclusive set of rows. You talked about filtering the form, and in the properties of the form I see where a filter can be placed, but unless I am looking in the wrong place (the properties for the control that needs the filter), I don't see where I can apply a filter to only that control. And the filters needed are mutually exclusive and so can not be applied to the whole form.
Thanks again,
Ted
Klatuu - 12 Mar 2008 20:13 GMT Okay, Ted, I think I have a better understanding of what it is you are asking.
No, you can't filter a control. But, as I said in my previous post, you can use the control's Control Source property for other things than binding to a record source field. If you are trying to present a summed value in a text box, you would want to use a DSum function to return the desired value. So the Control Source property would contain something like:
=DSum("[FieldToSum]","QueryOrTableName","Filtering Criteria")
Where Filtering Criteria would be like a WHERE clause without the word WHERE.
If you need to fitler based on a value in the form's current record, then you can use a reference to the control that contains that value
 Signature Dave Hargis, Microsoft Access MVP
> > Good plan, come to an Access newsgroup for help and bash the product. > > [quoted text clipped - 213 lines] > > Ted Ted - 12 Mar 2008 20:31 GMT > Okay, Ted, I think I have a better understanding of what it is you are asking. > [quoted text clipped - 232 lines] > > - Show quoted text - Thanks you. That was what I needed.
Cheers,
Ted
Stephen Lebans - 12 Mar 2008 20:02 GMT Ted here is previous post of mine on this issue:
Newsgroups: microsoft.public.access.forms From: "Stephen Lebans" <ForEmailGotoMy.WebSite.-WWWdotlebansdot...@linvalid.com> - Find messages by this author Date: Mon, 29 Nov 2004 20:52:36 -0400 Local: Mon,Nov 29 2004 7:52 pm Subject: Re: .jpg pictures are allowed to store as OLE objects? Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse
The format of the image file is completely independant of Access. It is the responsibility of whatever Image/Paint program is currently registered with WIndows to view the Image file types and act as the OLE server for the object inserted into the OLE object field. MS Photo Editor is generally used as the OLE server for Access. It must be setup and registered to handle the selected image file types.
Before I explain how to setup Photo Editor to be registeredd for the relevant Image file types one note of caution. Images embedded as OLE objects will quickly fill up your MDB. A small 40KB JPG can end up consuming several MB's in storage space. If you are planning on storing a large number of images or even a few extremely high resolution images then you should not be using OLE.
Why do Images embedded as OLE objects take up so much storage space?
In a nutshell: An Image inserted as an OLE object is no longer the same size as the original file. It is converted to a DIB(uncompressed Bitmap) and then wrapped in a EMF(Enhanced Metafile). THe EMF is then wrapped within the OLE stream. Further, if the original Image was not a Bitmap or Metafile, then another full size DIB preview is inserted into the OLE stream.
THe only reasons to use an OLE object field are if you require:
1) The ability for your users to Edit the Image in the Program that acts as the OLE server. 2) The ability to view the Images with a Form in Continuous View. 3) The Images are embedded for security concerns.
In reality, the enormous OLE file size "bloating" stops most users from inserting more than a few images. With the exception of viewing the images with a Form in Continuous view, coding solutions for the other 2 issues are available.
The most common solution is to simply keep the images in a seperate folder storing the filenames only in a Text field. Using the Current event of the Form or the Format event of the relevant section on a Report, place a line of code to load the picture into the standard Image control NOT the OLE Frame control.
Me.NameOfPictureControl.Picture = Me.NameOfTextBoxBoundToFileNameField
There is lots of sample code out there complete with error checking. Search GoogleGroups.
 Signature HTH Stephen Lebans http://www.lebans.com Access Code, Tips and Tricks Please respond only to the newsgroups so everyone can benefit.
>I am afraid I am a little rusty WRT MS Access (the last version I used > was MS Access 2000, and THAT was only to fix to referential integrity [quoted text clipped - 66 lines] > > Ted Klatuu - 12 Mar 2008 21:36 GMT Stephen,
I think you responded to the wrong post. I believe you wanted "Embedded Pictures" just below this post.
 Signature Dave Hargis, Microsoft Access MVP
> Ted here is previous post of mine on this issue: > [quoted text clipped - 125 lines] > > > > Ted
|
|
|