MS Access How to make a field in form invisible if it is empty - sql

I am trying to make a filed in my form become invisible if it's value is empty.
What I have is a form with individual records from a table, this has a sub-form which is linked by an ID, and it displays the results of a query.
This has three fields ones is the ID, a number and third is a date, Not all records have a date and therefore for the ones that do not have one I want to completely hide the date label and the empty text box; and make it appear only when a date is specified.
I'm guessing I might have to use some kind of WHILE statement in my query but I'm not sure whether that's right and how to do it.
I would appreciate your advice on this.
Thanks

You can add this code to the subform current event:
If (datafieldname & "") = "" then
datafieldname.visible=false
else
datafieldname.visible=true
End if
Where datafieldname is the name of the data field on the subform. This will only work if your subform layout is not tableview

Related

Show a name instead of an id

I currently have these options set in a combo box:
This box will show the projectIDs that a current company has which is set with the criteria. I just want to show the name of the project as people will not know just by the id. If I remove the check box for the projectID it will just remove that field completly and not show anything. I have debugged what companyBox.Value is and it is indeed a number for a company and if taken this id and replaced it with the following query which is built from the picture and it returns the results I want. I just cant get the values to show in the combo box.
SELECT projects.projectName
FROM companys INNER JOIN projects ON companys.companyID = projects.companyID
WHERE (((companys.companyID)=7));
Gives me all the projectNames where the companyID is 7
And as seen below that same query just gives me blank spaces instead of the names:
Ok, if you have say a query, then as noted, then you can use a "join" to pull in the other table (based on that "projectID").
So, for a general report etc., then your approach of using the query you have is correct.
HOWEVER!!!
For a combo box? They have this feature and ability built in. In other words you do NOT in general need a join.
The combo box has two parts:
A sql query that "drives" or "fills" the combo box. This can (and will be) of course based on the Projects table.
VERY important:
Our combo box is to save, store, put, use the project ID into a column in our CURRENT form. That form of course is based on a differnt table (the forms current table). So, keep in mind the two concpets:
Combo box can be driven by any table to display data.
Combo box will/can use a column from that "other" table to SAVE into a current column on the current form.
Now of course the Project "id" is the VALUE we want, but we sure as don't want to display that "project id" value, since as you note, humans want the nice looking text.
First Rule:
ALWAYS, but ALWAYS make sure the FIRST column of the combo box "sql query" is the column we WANT to save into the current form, but ALSO the column we are going to HIDE AND NOT show to the user.
So, a combo box can with great ease HIDE the first column. You can hide other columns, but as a general rule a VERY HIGH number of my combo boxs will have two columns. So, you might need company "id", but would want to display company name as nice text to the user, but store/save/use the "id" of company for this purpose.
So, in your case? Change the order of your columns.
You want:
ProjectID, ProjectName, and you ALSO can continue to have a filter based on company.
So, once you get above setup, and you WILL NOT need a sql join. Remember, the combo box has it own "whole sql statement" based on table projects.
So, the combo box will:
Save/store/use the ProjectID when you select a project.
And it will display the project name, but behind the scenes it will use + save ProjectID.
So, just make sure that you set the length of the first column in the combo box to 0 (to hide that projectID from display).
Next, make sure you set WHICH column value from the query the combo box is to save.
That column will be the FIRST column, so you want to set that to 1
Your combo settings will look something like this:
In above, the CONTROL source is your CURRENT form and table.
So, I want to get a Hotel "ID" from the table hotels, but I am going to save the results of the combo box selection INTO a column called Hotel_ID
And note VERY careful - I set the bound column = 1 (that is the FIRST value from the query that drives the combo box.
Next up:
We want to hide the first column value, so you need this setting in format tab of the property sheet for the combo box:
NOTE VERY close in above. I set the FIRST column width = 0. this is HOW you hide the "ID" of hotels - I only want the user to see the nice hotel name. (or in your case Project name).
But, the 2nd column, I have a width for the Hotel name (or in your case project name).
So, make your first column in the query for the combo box the "id" of project id. Set the width of the first column = 0.
So, the query that drives the combo box? It is based on the ONE table, but you need to ensure that the "id" that you going to use is the first column, and simple hide it from display.
So, a combo box has a sql query that drives the combo box, and that query in most cases will NOT be the current table.
So, you have to set BOTH settings (the column the combo sql query will use - but to avoid confusing, then just adopt the habit of making the 1st column the "id" or value you want from that sql.
But, after setting above, you STILL have to set what column to shove/put that column into on the current form. (that setting is the control source).
In effect, a combo box is a kind of look-up into the other table, and it can display nice user friendly text columns, but still use + store the ID.
So, your query should look more like this:
SELECT ID, projectName
FROM projects
WHERE (((companys.companyID)=7));
I don't know if your first column of Projects table is "ID" (or is it ProjectID), but as you can see, the combo box ONLY needs to be based on the one table, and with our new rule - we always use the first column of the query the "id" value we want.
Now, of course the above is "hard coded" for company, and your original query that drives the combo box was fine - just that the order of the columns display was incorrect.

How to create multiple connected search combo boxes to filter a form?

I have a form with 10 columns, and for 5 of them (Project_Phase, Contract, Design_DPM, AMM/UCC, 1_or_2 stat) I want to add connected drop list combo boxes to filter the records and display data as selected in the combo boxes.
I know how to make multiple drop list combo boxes that filter the whole form based on the selection of one value from one column. For example, "Contract" combo box has options: signed, not signed. If I select "signed" it will display all the records that have "signed". and If I filter another column it will cancel the previous filter and display records relevant only to that selection from that column.
But what I want is the ability to filter using any number of filtering options from the 5 columns I mentioned. For example, if I want to see the records that are ("signed" under "Contract") and in ("proposal" under "Project_Phase") and ("a certain DPM" under "Design_DPM"). And after filtering I want to be able to clear the filters and see all the records again, as I am using this form to display all my records for users. I do not want it to be cascaded, as I might want to filter using only one column or more or all. And I do not want it to be a query or use the basic filtering in datasheet view.
Sorry for the lengthy explanation, if something is not clear I will explain further. Thank you for your efforts.
Here is one way to implement a form filter using multiple comboboxes.
Create a table named 'tblWildcard'; one field named 'Wildcard', save the table.
Enter one record with an asterisk as the value
Create the queries for your comboboxes like:
SELECT DISTINCT Table1.flda
FROM Table1
ORDER BY Table1.flda
UNION Select wildcard from tblwildcard
Save a query like the following SQL to be the Row Source for your form (i.e. qryFormA):
SELECT Table1.Flda, Table1.Fldb, Table1.Fldb
FROM Table1
WHERE (((Table1.Flda) Like [Forms]![frmForm1]![cboFlda])
AND ((Table1.Fldb) Like [Forms]![frmForm1]![cboFldb])
AND ((Table1.Fldc) Like [Forms]![frmForm1]![cboFldc]))
In the After Update event for each combobox add the following code:
Me.Recordsource = "qryFormA"
Sometimes the 'Me.Rowsource.' may not work when changes are made (weird issue!). If so, do the following:
Application.Echo False
Me.RecordSource = vbNullString
Me.RecordSource = "rowVwFilter25"
Application.Echo True
Finally, if a multi-user environment, and people may be adding records that will need to be included in your comboboxes, use the combobox Before Update event and add the following code:
Me.cboFldA.Requery
My question is answered. I found exactly what I wanted here link
Thanks for the help everyone

Ideas for diplaying query results next to buttons

I have a temporary table that is created from a series of query with a VBA code. The name and number of fields in the table changes with the results of the queries.
Right now I simply open the table with DoCmd.OpenTable.
What I would like to do is display the table in a form so I can include some buttons for example to allow the user to export the table.
I tried a listbox but the formatting was not good and I can't add horizontal scroll bar to see all fields.
I tried a subform populated from the table, but it would not adapt to the changes of the table (fields and numbers).
Any Idea of what can I do next ?
the subform seems to be a good idea. When your table is ready to be shown, you can assign it to a subform object via Me.subFormName.SourceObject = "Table.tableName"
(of cource "subFormName" and "tableName" have to be replaced by the actual names of the subform and the table)

how to populate textbox with the database value based on combobox change in access

I am new to Access and vb and i have failed to get the result what i am thinking to get ,hope i will resolve with your help guys
->my access database contains one table with following fields
firstname lastname middlename phone
I have two combo boxes and two textboxes
combobox- it populates firstname and based on this selection the other combo box has to fill with last name which i did successfully with the help of Me.Combo2.Requery
but i dont know how to fill middle and phone in textboxes
here i am failing in mapping recordset and connectionstring with my table and getting result to textbox value can any one help on this
You still have not said where the connection string comes from or why. The big advantage of Access is that it is fast and easy, for the most part, you do not need connection strings.
First, select your table, then choose create form. This will create a single form bound to the table. You could have created a query first and chosen that instead.
That is it. You have a form that displays your data. The key to this is the property sheet for the form, which you can find by switching to design view and double-clicking the small square at the top left of the form just under the form name. The property sheet will show the name of the table or query on the Data tab under the Record Source propery. You can, of course, set this property manually.
You can now add a combobox to select records, but you do not have to, there are navigation buttons at the bottom of the form.
To add a combobox that selects records on your form, you must have first bound a recordset to your form using the steps above. Next, ensure that the wizard is selected for the toolbox. Unless you have unselected it, it will be selected. Now choose combobox from the toolbox, it will open the wizard.
Choose find a record on my form and press next to choose the relevant ID field / column and any other fields that you need. When the wizard completes, in MS Access 2010 (and probably in 2007) it will add an embedded macro that finds your record and in earlier versions it will add code. Other properties set by the wizard will look something like this:
Row Source : SELECT ID, Firstname, Lastname FROM Table1
Column Count : 3
Column Widths : 0cm;2.54cm;2.54cm
Your table should have an ID, add one before you start, if it does not. The wizard will not add a Control Source when this option is selected because it is not needed, in fact, it would be a disaster to add a Control Source to a combo that finds records. Once again, you can do this manually. The code to find a record might look something like:
Private Sub MyCombo_AfterUpdate()
With Me.Recordset
.FindFirst "ID=" & Me.MyCombo
End With
End Sub
This works because the bound column of the combo is set to 1, the first field of the select statement, which is ID.
The combo includes two other fields / columns and you can refer to these in a textbox by setting the control source of the textbox to:
= MyCombo.Column(2) ''Lastname
Where you count columns from zero:
Row Source : ID (0), Firstname (1), Lastname (2)
Cascading combos are another story altogether, and you can read it here How to synchronize two combo boxes on a form in Access 2002 or in Access 2003 and here Is there a simple way of populating dropdown in this Access Database schema?

How to get min/max values on form that returns multiple records

I've got a form that returns multiple records from a table. Each of these records has a field indicating what the measurement was and the measurement's value. My form's record source pulls the appropriate ID's set of measurements and values, and then I want to write each of these measurements to different text boxes on the form.
The problem is that I'm also keeping track of the date this data was updated, and want to include only the data pulled based on the "date updated" selected at the top of the form.
When I go to pull the measurement value into a text box (with something like max(iif(Measurement="Acidity", [Value],[Null])) I get #Error all down my form. This had been working earlier today, and as I've been developing the form something changed and now it no longer works (everything but the date returned is saying #Error). I've set it to Requery when the form loads and whenever the date selected is changed.
EDIT:
This form is actually a subform of another main form. The main form has a control called MemberID which a user can use to select the Member they want information about. This is based on a query that pulls from a table the information related to that MemberID (one row per MemberID). When the form loads, the query behind the main form needs to be requeried to get the data for the selected MemberID.
Then there is this subform causing the problems. On this subform is a "Data Updated" drop down box, which lists the possible dates that the data was updated. When one chooses a particular "Data updated" in the drop down box, this chooses the latest data for all the data measures up to that date selected. This means the form's record source pulls about 10 records in my case.
Then, on this subform I try to layout these 10 records' values in different text boxes. So, in my acidity case above, I would say iif(Measurement="Acidity", [Value], Null). Because there are 10 rows returned, I will end up with 9 Nulls and 1 value, and to get that value I use the max function, so the text box's control source reads: max(iif(Measurement="Acidity",[Value],Null)).
The strange thing is this code was working this morning, until later this afternoon when I was filling in the remaining text boxes with similar code, and then it stopped working. I had also renamed the boxes from Text71 to more relevant names, but none of these are referenced in the code.