passing parameter from access into sql-server - sql

REPHRASING QUESTION:
i apologize for being unclear. i have a textbox and a button a form. when the button is clicked it runs this saved access query:
select * from sqlservertable where field=form!textbox.value
i have an access front end and sql server back end.
this query is not working. it doesnt like me passing this parameter form!textbox.text. is there an alternate way to pass the value of the textbox?

The .Text property of an Access control is available only what that control has the focus.
Secondly, in Access, you refer to forms as members of the Forms collection, i.e., the collection of open forms.
So, you use:
Forms!FormName!ControlName
Without specifying the Forms collection as the container object for the form, the Access expression service won't know where to find the control.
And as .Value is the default property of controls, you don't need to specify it, though if you want to be really picky and explicit and type more characters for no actual benefit in this context, you could use:
Forms!FormName!ControlName.Value
But that won't behave any differently at all in this context (the only situation where it will is if you're trying to force evaluation of the control before passing it as a parameter of a subrountine, in which case without .Value you may end up passing a control reference instead of the value, which could be bad or it could be fine).

Have you tried setting the RecordSource property on the form (from code) with:
Form.RecordSource = "SELECT ... FROM ... WHERE [Occurrence Date] BETWEEN " & Text1 & " AND " & Text2

Related

Updating Properties of a Report - OrderBy and Filter

I need to have another form do sorting and filtering but I can't seem to change the properties and I am getting an error 438 (Object doesn't support this property or method)
Looking for a possible alternative.
[Forms]![Form-Main]![zSubForm-1]![SubFormReport-Form1].OrderBy = "[Form1_Number] ASC"
[Forms]![Form-Main]![zSubForm-1]![SubFormReport-Form1].Filter = "Me.txtFilterBox"
I'm triggering this via a Button.
Reference subform/subreport through container control but don't actually use subform/subreport name. Don't put reference to textbox within quote marks. If field to filter on is text type, use apostrophe delimiters.
Syntax to use is:
Forms!mainformName!subformcontainerName.Report.OrderBy
Forms!mainformName!subformcontainerName.Report.Filter
Consider:
[Forms]![Form-Main]![zSubForm-1].Report.OrderBy = "[Form1_Number] ASC"
[Forms]![Form-Main]![zSubForm-1].Report.Filter = "somefieldname='" & Me.txtFilterBox & "'"
Be aware any Sorting & Grouping settings in report design will override OrderBy property and code will be ignored. Filter can still be programmatically changed.

How to use Form Controls in Modules

In my modules, I want to use my controls from my form. For example: I want to set focus on a textbox after a certain Sub.
My current solution is to make a subroutine to set all controls in a public variable (see below).
My questions:
What is the best practice? How do people usually do this?
When should I call my subroutine? (is it the first call in the FORM_LOAD sub?)
Public TBnr As TextBox
Public Sub controlsInitieren()
Set TBnr = Forms("frm_TreeView_Example").pstNr
End Sub
Well, as a general rule, while many platforms seperate out the UI part and the code part? Well, Access is quite much a different approach - it is standard fair to place the required code inside of the form "class" (all forms in Access are a "class", and you can even have muliple instances of the SAME form open more then one time).
So, in general, your code should be in the forms code (class) module.
However, you could and call a external routine.
So in the form, you could call your above routine like this:
Call MySetFocus(me, "NameOfControlToSetFocusTo")
And your sub would look like this:
Sub MySetFocus(f as form, sCtrl as string)
f(sCtrl).SetFocus
End Sub
However, as noted, the amount of code above is more code then simply in the forms code module going:
me.ControlName.SetFocus
However, while the above is a less then ideal example, passing the form "instance" (me) to a external sub or function allows you to referance any property or method or feature that exists in the form in an external routine.
So in place of say
me("LastName") = "Zoo"
In the above sample routine, you would and could go;
f("LastName") = "Zoo"
So any place you would and could use "me" in the form, you can use the form instance you passed from the form. As noted, it is a good idea to use "me", since as I noted, Access does allow multiple copies of the form to be opened at the same time - and thus your code can't distinguish between what form instance you are using unless you pass the current "in context" form. So like in JavaScript, using "this" ?
In access that current instance of the class object is "me", and you are free to pass that instance to any sub or function you want as per above.
The best practice is to use only procedures inside the form code. In such a case you refer to a text box control in this way: Me.Textbox1.SetFocus. If you want to set some controls properties during the form loading, you can do that in the frm_TreeView_Example_Initialize event;
They usually do it in the way I described at item 1;
If you want to use such a strange/unusual way you can do it calling the subroutine whenever you want. But, in order to set a specific property value of the form frm_TreeView_Example controls you can simply use frm_TreeView_Example.TextBox1.SetFocus. You can use this way of setting in a module procedure, even before the form has been shown. You can simply show it in that procedure code using at the end: frm_TreeView_Example.Show;

How to requery a form from navigation form?

I've struggling with this problem on my own, then with some help, then search about it; but I haven't had any luck. So I decided to ask
I have two forms in access 2007.
1. frmNavigation
2. frmOrderList
The form frmOrderList is embedded in the form frmNavigation. The form frmOrderList is built based on the queris which have criterias located on the form frmOrderList.
When the criterias are change the macro (after update) start run and trying to refresh the the frmOrderList.
I tried to serveral codes but it dosen't wortk for me.
Forms![frmNavigation]![NavigationSubform].Form![frmOrderList].Requery
Forms![frmNavigation].Form![frmOrderList].Requery
Forms![frmNavigation]![frmOrderList].Form.Requery
Forms![frmOrderList].Requery ' Works fine for the single form
Still the same error
Microsoft access can't find the field 'frmOredrList' referred to in your expression
It is good cheatsheet in russian - http://www.sql.ru/faq/faq_topic.aspx?fid=156 I'd like to translate some points:
Basic syntax is this:
Forms![Form1].Controls![Field1].Value
Mention, that "." and "!" goes one by one, switches.
It is possible to do like this:
Forms![Form1]![Field1]
But there should not be same named different collections' objects.
Addressing subform
Correct link to the property of subform or subreport needs to address full form identifire, using Form property of control:
Forms![Form1].Controls![Form2].Form.Controls![Field1].Value
In this case:
Forms![Form1].Controls![Form2] is a link to a control, where subform is displayed.
Forms![Form1].Controls![Form2].Form - is a link to subform. Addressing Form property is required for MS Access 97, and optional for next versions.
As described, addressing to 3rd and more level subforms is constructed:
.Controls![Form2].Form (or .Controls("Form2").Form )
For addressing to an object in current context
It is recommended to use following syntax:
Me.Controls![Field1].Value
NB: name of control, that contains form can be different from form's name. It can be checked through studying .Name property of a control.
So using these instructions, your code should be following (see 2nd recommendation):
Forms![Form1].Controls![Form2].Form.Controls![Field1].Value
in this case
Forms![frmNavigation].Controls![NavigationSubform].Form.Controls![frmOrderList].Form.Requery
And this will work, only if only names of your controls are same as Form's names, otherwise you should correct names of control names, that are in squre brackets.

Losing ability to change properties when changing SourceObject of subform in Access 2007

I have a subform that changes where it gets its data from based on user input by using the Me.SubForm.SourceObject = Query.SomeQuery. It seems that by doing this, I am losing the ability to set the BeforeUpdate property.
The code I am using is as follows:
Forms![PartsDatabase]![RepsSubform].Form![Pack Rank].BeforeUpdate = "=ToTracking()"
I have confirmed that this works before the SubForm.SourceObject is changed, but afterwards it throws the following error: RTE 2455 "You entered an expression that has an invalid reference to the property BeforeUpdate."
I was wondering if this is a known issue or if I just need to modify my code to adjust.
You are getting things muddled up here. You should never be changing Source Objects, rather you should be changing the Record Source. The code involved in the Form is Form level. If you wish to use the Before Update event, it belongs to the Form and not the Recordsource. So you always go to change the RecordSource.
You would use,
Forms!Parentform!SubForm.Form.RecordSource = "SELECT someFields FROM someTable;"
Or,
Forms!ParentForm!SubForm.Form.RecordSource = "yourCompiledQueryName"

Why can't I get properties from members of this collection?

I've added some form controls to a collection and can retrieve their properties when I refer to the members by index.
However, when I try to use any properties by referencing members of the collection I see a 'Could not set the ControlSource property. Member not found.' error in the Locals window.
Here is a simplified version of the code:
'Add controls to collection'
For x = 0 To UBound(tabs)
activeTabs.Add Item:=Form.MultiPage.Pages(Val(tabs(x, 1))), _
key:=Form.MultiPage.Pages(Val(tabs(x, 1))).Caption
Next x
'Check name using collection index'
For x = 0 To UBound(tabs)
Debug.Print "Tab name from index: " & activeTabs(x + 1).Caption
Next x
'Check name using collection members'
For Each formTab In activeTabs
Debug.Print "Tab name from collection: " & formTab.Caption
Next formTab
The results in the Immediate window are:
Tab name from index: Caption1
Tab name from index: Caption2
Tab name from collection:
Tab name from collection:
Why does one method work and the other fail?
This is in a standard code module, but I have similar code working just fine from within form modules. Could this have anything to do with it?
Edited to add
formTab was declared as a Control, but I find that if it is declared as an Object then the code works.
This will probably solve my problem, but in the interests of furthering my knowledge I would be grateful for any explanation of this behaviour, particularly with regard to the difference in running the code in the different types of module.
This is a really great question. Your edit at the end of the post reveals a lot about how VBA works and what's going on here. I'm not 100% this what's going on, but I'll explain what I think is happening.
A Collection in VBA (and VB6, for that matter; same code base) is not strongly typed. This means that everything in a collection is technically an "object." In the .NET world (as of .NET 2.0), it's possible to have strongly typed collections so that you could say "everything in this collection is a Control object." In VBA, this isn't possible with a Collection.
In your first iteration, where you are referring to the item indexed in the activeTabs collection, activeTabs(x + 1) is referring to an object. When you tell VBA to look up .Caption of that object, it doesn't know what the underlying type is (I think), so it has to simply look to see if the underlying object type contains a property or method called Caption. As you can see, Tab controls do in fact contain a property called Caption.
In your second interation, where you are doing a For Each loop, I think the problem is that the Control type probably doesn't have a property called Caption, though different types of controls probably do. For example, a text box control probably doesn't have a Caption property whereas as label control does have a Caption property.
You have a few options to fix your second loop. 1) You could declare formTab as a Tab control (I'm not sure exactly what it's called). The Tab control should have a Caption property. 2) If every control in activeTabs is not specifically a Tab control (in which case, you should probably call it activeControls instead of activeTabs), you could check within your loop to see if the formTab is actually a Tab control. If it is, cast it as a Tab control and then call .Caption. Until you cast it as a Tab control, VBA won't know that it has a Caption property since a regular Control object doesn't have a caption property.
In the end, you can get away with using objects as in your first loop and letting the runtime figure out what to do, but that can give really bad performance. In general, it's better to work with your specific types in a strongly-typed language. It also helps to show in your code that you know specifically what you're working with rather than leaving it to the runtime to decide what properties and methods you can work with.