View Form Creation Code in Access VBA? - ms-access

Is it possible to view the form creation code in Access VBA? I don't mean the event listener code that is viewable in the VBE from View>Code in the form's Design View. I mean the code that is used to create the components, and add them to the form. I would like to programatically set Form properties; doing this from the Properties dialog is quite slow (and annoying).

Yes, it's possible to create a form to design.
Vba include CreateControl Method (see MSDN Office-11 Control )
And you can add to this controls any event procedure like OnClick, OnFocus, etc. (see MSDN Office-11 Event
You can manipulate the width, height, etc.. properties as simple as me.control.width
I developed some time ago a form that could customized OnResize event to create different views depend of screen resolution and use all of this.

In theory you could get the form definition from the system table where it is stored (MSysObjects), but it isn't in a format that is practical to work against.
In theory you could avoid using the graphical designer to layout your form and create all the controls/set their properties dynamically in the form_load event. However, this wouldn't be much of a time-saver.
Assuming the gist of the question is whether there is an XML like declarative way to define a form layout similar to WPF. The answer is no.

All (?) the properties can be set through VBA for both the form and the controls.
Sub FormInDesignView()
Dim frm As Form
Dim ctl As Control
Set frm = Screen.ActiveForm
frm.Caption = "New"
For Each ctl In frm.Controls
ctl.Name = "txt" & ctl.Name
ctl.ForeColor = vbRed
Next
End Sub

No, it is not directly possible to do what you want. But if you insisted, you could use the undocumented Application.SaveAsText/LoadFromText commands and process the resulting text file.
Why you think this is a useful way to work in Access, I haven't a clue -- it's completely antithetical to the purpose and design of Access. Give up on your habits from other development tools and learn the Access way of doing things. You'll be a lot happier and productive in the long run.

Related

How can I dynamically select a Form Control in Design Mode?

I'm writing a code in MS Access/VBA to change another Form from Normal Mode to Design Mode, then trying to pick a TextBox control and change it into Combobox.
The problem I'm facing now, is that I can't find a way to SetFocus to this control. In Normal Mode I can just write:
Form_Name.Control.SetFocus
But that code doesn't work when this form is in Design Mode. Is another way to Setfocus (via VBA) to this control , in another Form in Design Mode, like clicking manually in that control when editing in Design Mode?
**Edit: ** The correct would be Select the specific form control in Design Mode and not setting it focus
You can include/exclude controls from the current selection in Design View by setting the InSelection property on each control.
The following VBA code deselects all controls and then selects the specified control:
Function SetSelection(frm as Form, CtlToSelect as Control)
Dim ctl as Control
'Deselect All Controls
For Each ctl in frm.Controls
ctl.InSelection = False
Next
'Select Specified Control
CtlToSelect.InSelection = True
End Function
For more information, see the InSelection Property documentation on MSDN (even though this is for Access 2003, it also applies to later versions).

Access form edit doesn't save when closing with acSaveYes

I can't get Access to save a form I have edited using VBA to hide (or unhide) a column. The basic code is below. I open the form (in Datasheet mode because I am changing the ColumnHidden property), make the change, and close and save, but it doesn't save the change. Experimenting, I changed acSaveYes to acSavePrompt, and it doesn't even prompt me to save. Does this have anything to do with being in datasheet mode? Never had this problem making similar edits in design mode. If so, how do I make and save such a change. Thanks for any ideas!
DoCmd.OpenForm "Form1", acFormDS
Forms![Form1].MyColumn.ColumnHidden = True
DoCmd.Close acForm, "Form1", acSaveYes
Changes you make using VBA are seen as temporary by Access, and not saved, even if you explicitly save your object. This behavior is intended (and very useful, as it allows you to hide things on the fly based on whatever you want without you having to worry about reverting changes after the user is done with the form). An exception to this is when the user first lets VBA make changes, then switches to layout view and makes additional changes, and then saves the object. In that case the changes made by VBA are saved.
As far as I know, you cannot change this. Obviously, you can make the change manually and then save it, or check if the column should be visible or not once you open the form based on something you set somewhere.

Auto-submit HTML Form using VB in Access 2007

I'm at my wit's end on this one.
Part of a project I'm working on requires interaction with an HTML form to bring up the correct page on an external web-based tool. I've managed to make this form accept entered variables and be generated on the fly using document.write, but what I haven't been able to do reliably is trigger the 'Submit' action in a way that will not throw off Error 91.
So far, I've been using a simple getelementbyid.click call... Here's a list of a few things I've tried to get it to trigger:
TestLoop:
Pause (1)
BrowserControl.document.getelementbyid("Submit").click
On Error Goto TestLoop
*(The 'Error Loop' tactic didn't really work at all.)
Pause (3)
BrowserControl.document.getelementbyid("Submit").click
(This actually works the best, but because different PCs might have different hardware specs and open applications that might slow it down, what works for me hasn't been working for another tester. If I make it TOO long, I end up adding additional delay to a form I'm already worried about being too slow.)
<script type="text/javascript">
window.onload = function(){
document.getElementById('Submit').click();;
}
</script>
(Embedding an auto-trigger in the generated HTML, frustratingly, works OUTSIDE of the embedded web browser control, but not inside it.)
Private Sub wb_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
If Me.BrowserControl.Document.URL= "about:blank" Then
BrowserControl.document.getelementbyid("Submit").click
End If
End Sub
(For some insane reason, using debug.print I found that it's saying the navigation is complete before it's actually complete, so this didn't work at all.)
I may have missed a few... I've been attacking this from a bunch of different angles. The trick is that I have to use the HTML form to interact with this system that I don't have admin priveledges on, so changing how the information is recieved isn't really an option.
My last train of thought was to see if I could mimic a FORM submission using straight VB, bypassing the need for a 'click' event, but it's been exceptionally difficult to search for things related to HTML FORM interactions with Access 2007 Forms. :P
Any assistance would be greatly appreciated!
Well, after 7 months of testing and thousands of activations in that time, splitting the 'work' seems to have done the trick.
Form_Open: Initialises the Browser Object and sets its address to about:blank
Form_Current: Calls a formula that 'draws' the web form using Document.Write, then enters a loop...
TestLoop:
BrowserControl.document.getelementbyid("Submit").click
On Error Goto TestLoop

Map Multiple Controls to one Method

I know in the .Net framework you can map any controls events to a singular method, as if you wanted to map a column of buttons in a GridView to one method, you could.
In VBA i have since forgotten this information. Could someone help me out in how i can map a Forms' Control(s) to one method in the Forms Code-Behind?
Current OnClick Events, to be consolidated
Private Sub btnSubmit_Click()
CloseForm Me
End Sub
Private Sub btnCancel_Click()
CloseForm Me
End Sub
There are at least two of these methods per form. On some there are 3 or 4 for various Events but they all have the same body, one line pointing to a global method.
It depends on what you want to do. You can set up the form so that it receives the click events and check the screen.active control or you can set the event line on the property sheet for each control to a function. Frequently, people who are not familiar with the ease of MS Access go a long way around, which is why it is useful to say what you wish to do, there is often an easier way and often a way that does not involve code at all.
On the click line of the property sheet for the control:
=Menu_Or_Exit([Name],"cmdClose")
of course you could pass Me as form, but I have found [Name] to be very useful in a number of ways.
An excerpt from the function:
Case "cmdClose"
DoCmd.Close acForm, FormName
CheckMainMenu
You can keep a menu form, a menu subform, or just copy the button from form to form.

Hide a column programmatically in MS-Access

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003?
For Example,
After user change event...
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.hidden = True
Else
ctl.hidden = False
End If
End If
Next ctl
What is the best approach to this type of challenge?
Is there a more obvious solution?
Controls do not have a "hidden" property (no objects in Access have a hidden property). They do have a .Visible property.
For future reference, I suggest you familiarize yourself with the Object Browser in the VBE -- open the VBE and hit F2. You can then restrict your search to the individual libraries used in your project. It does take a while to get to the point where you understand the object model, though.
Also, you can rely on Intellisense to learn the properties/methods of an object, so in the code of the form you're working with, you can type "Me.MyTextBox." and the Intellisense dropdown will show you all the properties and methods of that particular control. It doesn't work for a generic control variable (as in your code) because different control types have different properties.
And, of course, the properties sheet gives the names of the properties, even though in code they don't always use the same orthography (usually they are the same with spaces removed).
Also, there are differences in how you might want to do this depending on whether it's a regular form or a datasheet form. In a datasheet, your controls also have .ColumnHidden and .ColumnWidth properties (setting those in any view other than datasheet view has no effect, and neither of those properties are available in the standard property sheet, but changes to them are retained when you save the form).
I answered a similar question to this not long ago to do with hiding columns on a datasheet. However you seem to want to hide textboxes arranged in a column on a form, is that correct?
Iterating over all the controls in the form could be slow if you have many controls. If you really need to use textboxes like that, then you could try group the 'columns' together then hide the groups. Another approach would be to use a listbox or datasheet to represent the data, where you can alter the layout of the columns directly.
I found the ColumnHidden property does the trick.
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.Columnhidden = True
Else
ctl.Columnhidden = False
End If
End If
Next ctl
I got a hint from this related question.
A one-liner approach is using:
forms(fname).Controls(ctrlname).columnhidden = false
where
fname is name of your form
ctrlname is name of your control