VBA - The Form Class has no the show method - vba

I want create one form from another. But the Form class has no the Show method, which described at http://msdn.microsoft.com/en-us/library/office/gg251540.aspx
It's code in Form_Main:
Private Sub btnTemp_Click()
Dim frmOpt As Form_Option
Set frmOpt = New Form_Option
frmOpt.Show vbModal
End Sub
But I received the "Compile error: Method or data member not found".
Where I made mistake?
Thanks
(VBA version 6.5; Access 2007)
=====
Sorry for my previous comment: right now I see that comment isn't obvious.
I don't have subForm on my mainForm.
I have two simple form: Form_Main and Form_Option. And I want to be the next logic:
Form_Main has button "btnOption"
Click on "btnOption". The Form_Option is opening
I change options on Form_Option
And click the btnSave button on Form_Option, and the next idea is executing:
Form_Main.TimerInterval = CLng(Form_Option.edtTimerInterval.Value)
At the moment I made it simple. And that is enough for me.
I write so:
Private Sub btnOptions_Click()
' After changing options, refresh timer interval of main form
DoCmd.OpenForm "Options", , , , , acDialog
Me.TimerInterval = 1000 * CLng(MOptions.loadOption("fPeriodVerifyNoticeInterval"))
End Sub
Where fPeriodVerifyNoticeInterval is parameter that stored in the options table.
And the Options Form changes the "fPeriodVerifyNoticeInterval" parameters at saving.
My problem is solved, Thanks

The "mistake" is that Show isn't a valid Method for Access Forms. The link you provided is for UserForms which are forms made in VBA.
If you want to create a new form that way what you want is something like this:
frmOpt.Modal = true
frmOpt.Visible = true
Though what I would recommend is doing this instead:
DoCmd.OpenForm "Option", , , , , acDialog which will open the Option form as a dialog.
Caution: If you create your form using New even though you set it as modal it will not halt the progress of VBA code. This means that your variable will go out of scope as soon as the code finishes. If you want your form to remain open, you will need to set it as static within the sub or declare it outside the sub like this:
static frmOpt As Form_Option
or outside the sub private frmOpt = Form_Option or public frmOpt = Form_Option

Related

MS Access 2013, Subform operations destroy the class module

I have a form in my Access project called MainForm, there is a sub form called subForm, also many buttons on the MainForm, at the same time, I created a class module to handle the OnClick event for all the buttons and the module name is classButtons.
Code in the class module:
Public WithEvents cButtons as Access.CommandButton
Dim tmpValue as String
Private Sub cButtons_Click()
Select Case cButton.Name
Case "ButtonA"
MainForm.subForm.Requery
Case "ButtonB"
Let tmpValue = subForm.ComboBox1.Value
DoCmd.RunSQL "update sometable set somefield='" & tmpValue & "'"
Case "ButtonC"
DoCmd.RunCommand acCmdUnhideColumns
End Select
End Sub
In the Open event of the MainForm, I have the following code:
For i = 0 to Me.Controls.Count - 1
If Left(Me.Controls(i).Name,6) = "cmdbtn" Then
set btnClass = New classButtons
set btnClass.cButtons = Me.Controls(i)
btnClass.cButtons.OnClick = "[Event Procedure]"
mdPublic.buttonColl.Add btnClass 'buttonColl is a collection variable declared in another module called "mdPublic"
End If
Next
Then once the MainForm is opened, all the 3 buttons works well, but once ButtonA or ButtonB is clicked, all the 3 buttons will stop working.
I tried to remove the subForm operations from ButtonA and ButtonB, and found that the problem is disappeared, so I guess the subForm operations just "destroy" the class module.
But I do need those operations, anyone has any ideas? Thank you !!!!!
What I do is make a function behind the form to handle the button behaviors, call it HandleButtonClick(x As String), then in each button Click event property:
=HandleButtonClick("ButtonA") -- change the button designation as appropriate
Also, I always name a subform container different from the subform it holds, such as ctrDetails. Code behind main form must reference the subform container name.

I need to open a specific page on a TabControl, which is on a form that has two TabControls

I have a Form named frmSearchAirport
I have two tab controls on the form named TabControl1 and TabControl2.
Each tab control has five pages.
On another Form i have command buttons.
I want to use each command button to open a page on a TabControl and to make the inactive TabControl invisible
I wrote this code for one of the command buttons so that it would open the first page on TabControl2 and and make TabControl1 invisible but i get an error message saying the action or method requires a form name argument
DoCmd.OpenForm FormName:=frmSearchAirport, View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False
and this code for the On Load event of the Form named frmSearchAirport
Private Sub Form_Load()
If IsNull(Me.OpenArgs) = False Then
Me.TabControl1 = Me.OpenArgs
Me.TabControl2 = Me.OpenArgs
End If
End Sub
Can anyone help with this.
Thank you
Edit 1:
Ive since changed the code to this and im getting an Application Defined or Object Defined error
DoCmd.OpenForm FormName:="frmSearchDublinAirport", View:=acNormal,OpenArgs:=0
Forms![frmSearchDublinAirport].Form.TabControl1.Visible = False
Edit 2:
ive changed the code to this. The form is actually opening on the correct page but the other TabControl is still visible and the pop up error message appears saying Application Defined or Object Defined error
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False
Edit 3:
This code is working. the problem was I named the TAbControl incorrectly. I used TabControl1 instead of TabControlOne
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControlOne.Visible = False
The FormName parameter of DoCmd.OpenForm needs a string - the form name.
So you probably need:
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0

Using VB .net to click command button in Access

I'm trying to click a button in a form in MS Access with VB .net. However, there isn't much I can find in this area and have a bit of a long way of getting the button. Then I'm stuck - there seems to be no way to activate the click event.
Using :
Imports Microsoft.Office.Interop
I have the following to get the button:
Dim acc As New Access.Application
acc.OpenCurrentDatabase("C:\path\to\db\aDatabase.accdb")
acc.Visible = True
For i = 0 To acc.Forms.Count - 1
If acc.Forms.Item(i).Name = "formName" Then
For j = 0 To acc.Forms.Item(i).Controls.Count - 1
If acc.Forms.Item(i).Controls.Item(j).name = "btnEnter" Then
Dim btn As Access.CommandButton = acc.Forms.Item(i).Controls.Item(j)
'
' click on button??
'
End If
Next
End If
Next
I've had a guess at trying the following:
acc.Application.Run(btn.OnClickMacro)
acc.Application.Run(btn.OnClick)
btn.OnClickMacro
btn.OnClick
btn.performclick()
none of which work.
By default, the click event handler is declared in VBA as
Private Sub Command0_Click()
' do something
End Sub
You won't be able to programmatically click the button outside of Access' VBA code if it's private (unless the developer intentionally declared it public). Best practice is to make a function which is called by the handler, which can be called elsewhere as well, instead of clicking buttons through code.
Private Sub Command0_Click()
DoSomething
End Sub
' you would have a better chance calling this from .NET
Public Sub DoSomething()
' do something
End Sub
If you don't have access to the Access VBA code, outside of .NET you could use something to automate mouse clicks like this: https://autohotkey.com/
I mean, if you have to have Access intslled, open and running, then why is such code not being run from Access? So it does not make a whole lot of sense to call such code from .net.
However, simply make the private sub click as public, and then you can use this form:
MyAccessApp.Forms("main").command44_click
So you don’t have to create any external functions etc., but if you wish to call that code directly then the form MUST be opened, and with a running instance of that application from .net, then the above syntax will work.
I would suggest you just use Access here, and it nots clear why .net is involved.

VB.NET Call Sub of another form

I know, that there are many questions related to this, but still I cannot find a workable solution.
Usually, it would work like this: A form creates an instance of another form in it's container like this:
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.TopLevel = False
PolInstIn.Name = "Sub From"
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
Me.GroupBox6.Controls.Add(PolInstIn)
PolInstIn.Show()
End Sub
Then it's easy to call a Public Sub from the sub form like this:
Call PolInstIn.MyPublicSubInSubForm1()
However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidental instance.
I suspect it is because my parent form is also an instance of an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).
Could you point to any simple solutions?
Regards,
Libor
I see this question got a lot of views, so here is an answer.
1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).
2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set
PollInstIn.ParentFormGrid = Me
This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.
make Private Sub LoadDetail() to a public :
Public Sub LoadDetail()
It work on my project. Hopely its what you want
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.Name = "Sub From"
PolInstIn.Show()
PolInstIn.TopLevel = False
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
PolInstIn.Update()
PolInstIn.Refresh()
Me.GroupBox6.Controls.Add(PolInstIn)
End Sub

Getting a Modal pop form to show up in Access

i'm trying to use the following code to open a Form using VBA
Private Sub cmdEdit_Click()
Dim d As New Form_EditNote
d.txtDate.Value = EntryDate.Value
d.txtNote.Value = Note.Value
d.Visible = True
End Sub
the form opens but only for a split second .. then goes away... any ideas?
You are declaring your variable d, assigning it to your form, you make the form visible, then your function is over and all your local variables are destroyed. Your form is one of these local variables. You need to use DoCmd.OpenForm EditNote. That will keep it open
To make it modal, either set the modal porperty to true at design time if you always want it to be modal. Or in DoCmd.OpenForm set the WindowMode to acDialog