default value for textbox in bound form - ms-access

I have a data entry form in an MS Access 2010 database which is bound to a table called CommunicationTable, which in turn contains a primary key CommunicationNumber, a foreign key ClientNumber, and a field called Level. When the user opens the form, the ClientNumber is automatically populated into a textbox called ClientNumber. I want the Level textbox to show a default value equal to the value that was recorded for Level in the most recent record (highest CommunicationNumber value) for the specified ClientNumber in CommunicationTable. How do I set this up?
Here is the code I am currently using in the Default Value expression builder for the Level textbox in the form's property sheet:
=DLookUp([Level],[CommunicationTable],[CommunicationNumber]=DMax([CommunicationNumber],[CommunicationTable],[ClientNumber]=[Me].[ClientNumber]))
This code is giving an empty value when I load the form with a valid ClientNumber. I thought this might be due to the DMax() function producing the current (blank) row about to be added to the CommunicationTable, but I get the same empty result when I use the following instead:
=DLookUp([Level],[CommunicationTable],[CommunicationNumber]=DMax([CommunicationNumber],[CommunicationTable],[ClientNumber]=[Me].[ClientNumber])-1 )
How can I change this expression so that it gives the most recent value of Level for the specified ClientNumber in CommunicationTable?
EDIT:
I tried the following, but it is giving a #type! error:
=DLookUp("[Level]","[CommunicationTable]","CommunicationNumber= " &
DMax("CommunicationNumber","[CommunicationTable]","[ClientNumber]=
" & [Forms]![Main]![NavigationSubform].[Form]![ClientNumber] & ""))
Note that the ClientNumber textbox in the form is itself populated using the expression:
[Forms]![Main]![NavigationSubform].[Form]![ClientNumber]

As I mentioned in my comment, you have to adjust your query a bit:
=DLookUp("[Level]","[CommunicationTable]","CommunicationNumber= " & DMax("CommunicationNumber","[CommunicationTable]"))
Update:
=DLookup("[Level]", "[CommunicationTable]", "CommunicationNumber= " & DMax("CommunicationNumber", "[CommunicationTable]", "[ClientNumber]= " & Me.ClientNumber.Value & ""))

Related

ms access openform where no data

I have a frmA, based on qryA.
Button btnOpenfrmB on frmA opens another frmB, based on qryB.
But this frmB includes some unbound textboxes, with data from qryC, i.e.
=DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]). If qryB has no data (records), Dlookup fields return errors and opening frmB, which includes these textboxes, ends with an error. I understand this behaviour of frmB (there is no value B, that's why an error), but HOW CAN I AVOID IT and correctly open frmB for entering the first record ? When qryB has at least one record, everything works OK.
Thanx in advance
Duski.
Just use Nz function if Field don't have, for instance 0 values:
=DLookUp("Field";"[qryC]";"[ValueC] = " & Nz([ValueB],0))
or iif function if Field can contain any value:
=iif(IsNull([ValueB]),"",DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]))

Dcount wont count records matching a number entered into a form control

I want to have a form / database that the data goes into automatically search for duplicate entries. Duplicates can either be by reference number (which contains numbers an a letter) or by name.
I have tried this dcount on the control source for [counter] in form "Add New" that is bound to database "tracker" to count reference numbers that are duplicates, so that anything higher than a "1" count in the database would be flagged, but i can't get it to to count based on the value that the user enters into [reference number] on form "add new".
=DCount("*","tracker","'[Reference number]'= '[Tracker]![Reference number]'")
I want it to search all records in the table "tracker", where [counter] on form "add new" equals the number of records in "tracker" that have the same reference number as the user enters into the form control [retention number].
I ave set the "on Change" property to [Event Procedure} to trigger this count anytime that there is a change on the form. What am I doing wrong? Open to other approaches to solving the problem as well.
Your syntax is a little out, this assumes [Reference number] is a text string, and that the control on your form is also called [Reference Number];
=DCount("*","tracker","[Reference number]= '" & Me.[Reference number] & "'")
If its a number remove the single quotes.
This also demonstrates why its a good idea to rename controls on forms so you know it is the control and not the field. So if your control was called txtRefNumber you would know what you were referencing (and take the spaces out of field names - it makes for lots of extra typing of square brackets and mistakes.
Further Edit : If you put in the after update event of your txtRefNum field the following Me.YourCountControlName = DCount("*","tracker","[Reference number]= '" & Me.txtRefNum & "'") It should update. Remove the control source from the YourCountControl

error 2115 on combobox selection

I have a combobox on a form and I'm trying to programmatically select one of the items in the combobox after I've run my SQL query on the access database.
I use the following code to iterate over the items and set the selected item:
'Make the appropriate location appear in the combobox
For i = 0 To cboLocations.ListCount - 1
If Me.cboLocations.Column(0, i) = locindex Then
Debug.Print "locindex: " & locindex & vbCrLf & " Me.cboLocations.Column(0, i):" & Me.cboLocations.Column(0, i)
Me.cboLocations.SetFocus
Me.cboLocations.ListIndex = i '<<< error 2115
Exit For
End If
Next i
As indicated, I keep getting error 2115: The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Access from saving the data in the field.
Neither of the properties for this combobox indicated in the error message is set to anything. So I'm stuck. Please advise.
programmatically select one of the items in the combobox
The way I've always done that is to assign something to the combo's Value like this ...
Me.MyCombo.Value = "target text"
Value comes from the Bound Column of the combo's selected row. (You can find Bound Column on the Data tab of the combo's property sheet.) Conversely, assigning "target text" to Value selects the matching row.
In your situation, I think you're trying to select the combo row which contains the same text as your locindex variable. And if that is true, then I think all you need is this ...
Me.cboLocations.Value = locindex
As far as when you do that, neither Before Update nor Validation Rule seems like the right choice to me. I suggest you do it from whatever code you're using to run your "SQL query on the database", immediately after the query.
You are probably colliding with the BeforeUpdate event.
Try using AfterUpdate.

Access ODBC Subform field size too small to edit

I have a form with a combo box that allows a user to filter data in a subform using
DoCmd.SearchForRecord , "", acFirst, "[Division] = " & "'" & Screen.ActiveControl & "'"
The user should then add data to the subform below.
I've two problems:
When the user tries to edit this data, they get "The field is too
small to accept the amount of data you attempted to add. Try
inserting or pasting less data". It doesn't matter which of the
fields the user tries to enter data into, they all suffer this
problem.
The "Division" field that has been filtered, shows up as #Error for
the new record line. I've set a temp variable for this (see below), so it should
be showing the value used for the filter, but does so with or without the default set.
TempVars.add "Div", [Combo5].Value
I've looked in my ODBC forms and all the data types/sizes etc look to be the same.
There's data already in this form, which can be edited, even though it's the same size, but no new records can be saved. As well as this I can edit data in the supporting table.
Answer
Thanks to #AVG and #WayneG.Dunn for help. I ended up using the following code and using the filter as suggested. I couldn't get it to work very well on the subform, so I hid the field and used that as a Master Field for the subform. Still getting and error message "field too small" and "#ERROR" in the new record label, but the form works, so I'm just going to use it as is. Code for reference:
DoCmd.SetFilter "Division", "Division = " & "'" & [Combo5].Value & "'"

how to overwrite a parameter default value in SSRS?

I'm using SSRS 2008 and I have two reports (A and B). B has 4 parameters for which I have assigned a default value. Both reports work fine on its own
The problem is that I want to be able to call report B from report A and use the selected value to overwrite one of those parameters in report B.
So far what happens is that Report B appears when it's called from Report A but the parameter that should be overwritten remains blank and causes the data not to load at all, the. The other parameters show the default values but the 4th one remains blank.
I have tried:
taking off the default value and didn't work.
order: I also read a post here about the order of the parameters and I chose to leave the one that needs to be overwritten last in report B.
data types match among the parameter being overwritten and the value being sent from the parent report (string).
the omit parameter expression is set to false which enables the value to pass through to the drill-down report.
Any thoughts?
I've done this recently by setting a click action in a tablix control. Essentially, I went to the Text Box Properties I went to Action and then set the action as Go to URL. I've then put an expression in for the following (which you can modify to your needs depending on the destination report name and parameters):
=IIf(
Parameters!p_hide_links.Value="y",
Nothing,
"javascript: void( window.open('http://' + window.location.host + '/ReportServer/Pages/ReportViewer.aspx?/Property+Management/" & replace(Parameters!i_this_report.Value, " ", "+") &
"&p_from_date=" & Parameters!p_from_date.Value &
"&p_to_date=" & Parameters!p_to_date.Value &
"&p_lease_exec=" & Fields!submitted_by.Value &
"&p_building_state=" & Parameters!p_building_state.Value &
"&p_division_id=" & Parameters!p_division_id.Value &
"&p_building_id=" & Parameters!p_building_id.Value &
"&p_view_type=d" &
"') );"
)