Relative name to get information from Form in MS Access - ms-access

I would like to know if it is possible in a Saved Query on MS Access to use WHERE clause getting value from a Form independent of MS Access installed language?
In a MS SQL QUERY WHERE clause that should be equal to a text in a form, I wrote it like this
= Form!Form_name!text_name
This work fine if all user have MS English version installed, however, this is not true in my case, as some users have a Portuguese version. In their version, the right way to call the same syntax should be:
= Formulários!Form_name!text_name
So, the problem is that Access does not recognize this considering language version. Due to this, if a user with Portuguese version try to run the query, it will pop up an insert box.
I've look throughout many sources but none could help me to solve this.
Thank you all in advance!

I can't test this as I don't have a portuguese version of Access but I avoid calls to form parameters because they are brittle. For instance the form has to be open or the query fails in wierd ways. If I must use a form's parameter I wrap the parameter in a public function (usually in that forms code behind). A public function usually gets picked up by intellisense even in the designer but a public variable does not.
My style is to synthesize a property with a private global scope variable and public get and set functions. Then in the designer you can call: get-myforms-property instead of Form!Form_name!text_name. When the form opens and closes I usually call set-myforms-property and set it to be equal to the actual form property.
In this way you also get the advantages of wrapping. For instance you can choose what value to return when the form is closed rather than just having the query fail.

Related

How to use textbox value as an SQL connection string

I am using VB.net in creating this application. My idea is when the main form loaded, there is another form with textboxes for the user to initialize connection to the sql server, and use that textbox value in calling the database for query.
Now, my problem is then main form closes the value in the textbox will be erased, and i have to type it again. When connecting for an SQL Server over the LAN, does it have the same process?
You should not be referencing controls' values for logical uses like that; instead use the value in the control to populate a more persistent program entity, such as a settings object.
In your particular case, with VB.net (and presumably WinForms), showing connection form with ShowDialog() allows the form to persist after it has been closed. You can add a public property to the form to expose the user-entered value to the code that called it, and DialogResult of OK when the user indicates the value is accepted (usually through a button click).
I'd provide a code example, but I've been deep in C# for more than a few months now and my VB.Net is getting rusty.

Differences in referencing forms in VBA Access

Intro:
I have two separate forms.
frmABC
frmXYZ
I have frmABC in focus and I run a procedure that will "Tick" a checkbox called ChkConfirmed on frmXYZ.
Question:
What is the difference between these two ways of referencing another form?
1. Form_frmXYZ.ChkConfirmed = True
2. Forms!frmXYZ.ChkConfirmed = True
For some reason, only #2 works in this scenario. For me this is bewildering purely beacuse I have always used #1 and it has never complained before (when I say complain, I mean it does the job).
But, when I use #1 here and step through the code, it definitely runs the code but does not check any boxes...just nothing.
So, I would love to know the technical differences to help me understand when to use each one and in what cases.
EDIT:
Actual code snippet (as requested)
#1 Version
#2 Version
Access (I think >=97) treats Forms as a Class which means your forms are now a class module and can have all [class behaviors] including instancing.
Form_your_formname: you are referencing the Form via the Class module.
Forms!your_form name: You are referencing the form by its form name.
To access a form via Forms!form_name, the form must already have been loaded. Otherwise the form won't be reachable and you will get an error message "the referenced ...... not found"
On the other hand, Form_form_name can be accessed at any time as a class. It can have multiple instances like all classes do. Accessing an unopened form via its class module will cause the form is being instanced. This means Access will create a new hidden instance of that Form.
To test this try following.
Create a new form called: frm_test with a text box in it called: txt_id
go to your immediate window and try following code:
Edit forgot to mention. The test form hast to have a vba module attached to it then only it becomes a class module
Form_frm_test.txt_id = 1
Form_frm_test.visible = true
?Form_frm_test.hwnd
docmd.openform "frm_test"
?Forms!frm_test.hwnd
Now you will see two instance of the frm_test form and each of them will have their own window handle.
to answer your question:
But, when I use #1 here and step through the code, it definitely runs the code but does not check any boxes...just nothing.
its because your form is being instanced and its hidden.
So, I would love to know the technical differences to help me understand when to use each one and in what cases.
Technical explanation given above.
if your form is already loaded you can use the Form!form_name / form_name to access it. If you are instancing use the class name.
To be honest, I've never seen your 1st example used, ever... In fact a quick test in Access 2010 fails with an "Object Required" error message. All the code I've ever done uses the exclamation, which (someone correct me if I'm worng) works in all circumstances where you would need to reference another form for something.

MS Access 32 bit converting to 64bit error

I wrote an MS Access app in Office 2007 and am now trying to modify it to run on Office 2010 64 bit version. I get the following error:
The expression On Open you entered as the event property setting produced the following error: Private Enum and user defined types cannot be used as parameters or return types for public procedures, public data members, or fields of public user defined types.
The error occurs if I try to open a form that has no code in its Open event, so I'm not sure where to start looking to make changes.
Thanks!
These sort of strange errors can arise if there are broken references. In the database open the VB window, on the menu choose Tools -> References. In the dialog box the references required will be at the top, check to see if any have "MISSING" prefixing the name.

How can I save a global variable value after dynamically loading a reference?

I have a Access 2003 database that will dynamically load MDB databases as a library reference. The reason for this is this database is a menu front-end for 60+ application databases. Rather than deal with permanently referencing all these databases, the menu front-end will dynamically reference what is needed when the user makes a selection. I was working on moving this database to Access 2010 and creating a custom ribbon. I started using the technique from here to capture the ribbon object in a global variable when the ribbon loads. I then ran into the problem where I could verify the code was running and the global variable was correctly being assigned the ribbon reference but after the database would run through it's startup routine, that global variable would get reset to Nothing.
To verify what was going on, I created a simple database for testing. In this database, I had a module with a global variable:
Public obj as Object
I then had a function like this:
Public Function SetObj()
Set obj = Application
Debug.Print "IsNothing=" & (obj Is Nothing)
References.AddFromFile "Test.mdb"
Debug.Print "IsNothing=" & (obj Is Nothing)
End Function
Obviously, in my code, "Test.mdb" refers to an actual file. If I run this code, Debug.Print gives me "IsNothing=False" for both instances, but after the function finishes and if I wait a couple seconds, Debug.Print will give me "IsNothing=True". If I comment out References.AddFromFile, Debug.Print gives me "IsNothing=False" no matter how long I wait.
It makes sense to me that since Access has to re-compile the VBA code after loading the library that all global variables are reset. I've experimented with moving the global variable into a class, but since I then need a global variable for the class, the class variable then gets reset instead. I tried using a local variable in the function to save the value of the global variable, but it looks like Access waits a couple seconds after the code is finished running to do the re-compile, so that doesn't work either. Does anyone have any other ideas to accomplish this?
I don't really know if this will solve the problem for this kind of reference, but in general, I don't use public variables for this kind of thing, but instead use a STATIC variable inside your function. It would be something like this:
Public Function SetObj() As Object
Static obj As Object
If (obj Is Nothing) Then
Set obj = Application
End If
Set SetObj = obj
End Function
Then you'd just use SetObj as an object for using your application. In a production app, you'd need tear-down code, too, but I've omitted that here.
I doubt this helps, but your code struck me as rather inefficient and incomplete.
I figured out a solution to my problem, and thanks #David-W-Fenton, as your answer gave me the idea. I use your approach in a library database for caching frequently-accessed values that are stored in a table but don't change after the initial startup. Those values aren't lost every time the references change, and that's when the light bulb lit up.
The solution is to put the global variable in a library database. Access looks to be only resetting global variables in the database that the reference is being loaded into - which makes sense after thinking about it. So since the library database isn't the one being re-compiled, it doesn't get it's global (or private or static) variables reset.
What I ended up doing was creating a new module in an existing library database. It has a private variable and two methods - one to set the variable, one to retrieve the variable value. In my menu front-end database, when the ribbon loads and calls my callback function, rather than saving the ribbon object in the front-end database, I pass it to this module for saving. I now no longer lose that ribbon reference whenever new databases are added to the library references on the fly.

ms-access vba Eval function behavior

I have a public function in an Access form
Public Function PopupProcess() as long
MsgBox Me.ActiveControl
PopupProcess = 1
End Function
When I call
eval("forms('MyForm').popupprocess")
it shows a message box 2 times. Does anybody know why it does that?
I Have Access 2003 with SP3.
EDIT : Main idea is to call function from that form for Custom Commandbar control OnAction.
Maybe you have better way to call function from a form for commandbar control.
This is a very long standing bug that’s been around since the days of access 97, (about 4-5 versions of access).
The solution here is to NEVER use the forms qualifier, simply place the following in your on action event, and you’ll be just fine
=PopUpProcess()
Note that you must precede it with=, and the suffix must have the brackets ()
Keep in mind that you can actually use behavior to your advantage. The function that runs is going to be from the form that currently has the focus on the screen. That means you can have different forms with the same name of the function, and whichever form has the focus, that function with that name will run from that forms code module.
Even better, if one of the forms does not have that function as public in the forms code module, then the function in a standard code module is used. So you might have nine forms, that all use the standard one function in the main standard code module. However, the 10th form might need to run special code, so you simply place that function code in the form’s code module as public and it will run in place of the public on in the standard code module.
This approach allows you to build a single custom menu bar that applies to many different forms, but those many forms will run different code on from that custom menu bar. This also encourages you to place the menu code in the form it belongs.
So to solve your problem, simply don’t use a form’s qualifier, and use the above format.
Note that you can pass Parameters from those functions also, for example
=PopUpProcess(‘hello’)
And then declare the function as:
Public Function PopUpProcess(strParm as string)
Keep in mind that the function and syntax and all of what I stated above also applies to when you use the on action in a ribbon for access 2007.
No idea. What happens if you call it like this?
Call Forms("MyForm").PopupProcess
Try using the CallByName function, instead of eval, to call your function. It should only fire your function once, and it will still allow you to parameterize the form name and the function or sub name:
CallByName Forms("MyForm"), "PopupProcess", VbMethod