How to hide a Visible Object in MS Access? - vba

I have a button named btnSave that by default is not visible and when I clicked the button btnAdd, this is the time that btnSave showed. My aim is to make this button invisible again when I clicked it but I am getting this error:
Run-time error '2165':
You can't hide a control that has the focus.
This is how my code looks like:
Private Sub btnSave_Click()
....
Me.Refresh
Me.Repaint
Me.btnSave.Visible = False
Me.btnCancelSave.Visible = False
End Sub
Any help is much appreciated.

The error message is clear - object cannot be set invisible while it has focus. Move focus elsewhere.
Me.someothercontrol.SetFocus
Me.btnSave.Visible = False
Me.btnSaveCancel.Visible = False

After several googling this is what I found and thanks to the Author (Mike Wolfe). Here is the link: Fix for the error: "You can't hide a control that has the focus" in Microsoft Access

Related

Word VBA - easily remove form frame?

I'm trying to create a user entry form which both captures the users input and displays a status update message. The slickest way I think of doing it is to have my modal form for the user entry display over a modeless form. After the user enters their info and clicks OK, the info from the modal form is copied to the modeless form, the modal form is closed and status updates get pushed to the modless form as things change during processing:
Hopefully, with a lot of messing about with positions, it will look relatively seamless. My challenge is getting rid of the frame on my modal form. I've done a lot of searching and it seems to involve completely redrawing the form from base libraries - is there seriously no easier way to do it?
I would not use a 2nd form but just place a simple Frame on top of your form. When you want to show the "modal form", just set the visibility of the frame to True, and when you want to hide it, set it to False - all controls (in your case, the input field and the OK and Cancel button) that are placed on the frame are automatically shown or hidden.
If you have controls outside the "modal form" frame that you don't want to be active at that time, set them to enabled = False. You could handle this with a simple routine within your form.
In this example I have a frame FrameModal painted on top of the form. Note that this frame could be places over other controls.
Option Explicit
Private Sub UserForm_Activate()
showHideModalFrame False
End Sub
Private Sub buttonShowModal_Click()
' Show the "modal" dialog
showHideModalFrame True
End Sub
Private Sub buttonOK_Click()
' Do your stuff here...
Me.tbUpdates = Me.tbUpdates & vbCrLf & Me.tbInput
' Hide the "modal" dialog"
showHideModalFrame False
End Sub
Private Sub showHideModalFrame(show As Boolean)
Me.FrameModal.Visible = show
Me.buttonShowModal.Enabled = Not show
End Sub
Start the form
Click the show button:

Multipage error: The object invoked has disconnected from its clients

So I've probably spent the last week researching/trying to fix this annoying error but to no avail. Here's what I'm trying to do and the error I'm getting:
I have a userform that contains 12 multipage pages and to access each one I have coded previous/next buttons. Now, One of these pages is disabled through properties to hide from the user, that is until a specific checkbox is clicked. When it's clicked, the page is enabled, and the user is now able to view this page as well as all the others. when the checkbox is clicked again (making it false and no longer true), the page hides from the user's sight. This is basically what I'm trying to accomplish. The pages are like so:
page1, page2, page3, page4, "page5", page6, etc.
As you can see, page5 is what is being disabled/enabled. Now also, something important to note, I've made the userform to appear before the workbook is actually visible using this:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
ThisWorkbook.Application.Visible = False
OpeningWindow.Show
Windows(ThisWorkbook.Name).Visible = True
Application.Visible = True
Application.ScreenUpdating = True
End Sub
This I would like to avoid changing because it's vital that the userform appear first before the workbook because the workbook is acting as the storage/database for everything that is being typed in the userform using this multipage system. Now when I run the userform through VBA and test out this function "checkbox_click Enables/Disables multipage page", it works no problem. When I attempt to open it (as if you were starting up excel without anything opened) everything works fine, until I click the checkbox in question to enable the multipage to make it visible. This is where I'm getting the error "The object invoked has disconnected from it's clients". Now I've tried some other things out to see if I could get the same result:
Made the multipage page visible = False instead of Enabled = false.
Result: This somewhat works, however when attempting to click the previous button to go back a page (Page6 to Page5) when Page5 is visible = False, The previous button doesn't respond, as if it knows that Page5 is there even though it's invisible.
Anyway, to wrap things up, I would like to ask the community here if anyone knows exactly why, from the code I've provided below that is responsible for this "page enable/disable feature", I'm getting this object invoked has disconnected error and if there's a way to fix it.
Private Sub CheckBox119_Click()
If CheckBox119.Value = True Then
Me.MultiPage1.Pages(5).Enabled = True
CheckBox138.Value = True
Label309.Visible = True
Else
Me.MultiPage1.Pages(5).Enabled = False
CheckBox138.Value = False
Label309.Visible = False
End If
End Sub
CheckBox138 btw is located on Page5 and is there if the user wishes to click it to disable page5 and Jump to Page4, which is this code:
Private Sub CheckBox138_Click()
If CheckBox138.Value = False Then
MultiPage1.Value = 4
CheckBox119.Value = False
Label309.Visible = False
End If
End Sub
Also, I'm relatively new to coding in VBA, but I'm always ready to learn.
After some playing around, I believe I figured out what the problem was. The checkbox138 in the disabled page was the culprit. By deleting the code Private sub checkbox138_click(), it works now. I'm not entirely sure why this is the case (so someone with more knowledge may be able to explain) But When checkbox119 is clicked, checkbox138 is suppose to turn true being that's what the initial code expressed. However, even though making checkbox119 true is suppose to enable the disabled page followed by making checkbox138 true, there seems to be a hiccup. It seems checkbox138 is thinking the disabled page is still disabled (even though checkbox119 is suppose to enable it) therefore making the checkbox138 hidden.

VBA - Closing the Opened form by DblClick Event, closes the whole running code?

I have a userform and some textboxes, I created a DblClick event to open and fill the second form according to the text box data. The problem is when I close Userform4, the whole running forms disappear! Nothing happens when I close userform3. But still, the mouse pointer shows the round status, like it is running a code!
I tried to change the Query_Close event of the second form to hide, but no luck. Can you please help? There is no Cancel button the Second form.
Private Sub txt1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Txt1.Locked = True Then
With ThisWorkbook.Sheets("target").Range("a1")
UserForm4.TextBoxJob2 = Txt1.value
UserForm4.TextBoxBadgeNum2 = .Offset(RowNumber, 3)
UserForm4.ComboBoxAC2 = .Offset(RowNumber, 0)
UserForm4.Show
End With
Else
UserForm3.Show
End If
End Sub
Regards,
M

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

VB.Net WebBrowser Navigate Only Working Once

Hoping someone can help me with this. I have two separate but related Forms, one of which contains a WebBrowser control. The user fills out some information on Form 1 and clicks a button with the following code:
If Form2Shown = False Then
Dim memoscreen As New Form2
Form2Ref = memoscreen
memoscreen.show()
Form2Shown = True
memoscreen.TopMost = OptionOnTop
Else
Dim memoscreen As Form2
memoscreen = Form2Ref
memoscreen.TopMost = OptionOnTop
memoscreen.QuickRefresh()
End If
The QuickRefresh sub in Form2 is the method that navigates. It is called both when the form is loaded as well as manually in the code above:
Public Sub QuickRefresh()
Dim HM As Form1
HM = Form1Ref
Me.Text = "retrieving information..."
Me.AxWebBrowser1.Navigate("SomeValidURL")
HM.Focus()
HM.SetHugoFocus()
End Sub
The problem I'm having is that the first time QuickRefresh is called (i.e. when Form2 is loaded) the navigation is successful and the page displays fine. If I then click the button on Form1 again, the page does not change. The Text attribute and window focus does change however, so I know the method is firing.
Some things I've tried/checked:
AllowNavigation is set to True on the WebBrowser control
Have tried looping while the browser is busy while calling Application.DoEvents()
Any suggestions would be appreciated. Thanks.
From your "Internet Options dialog > General Tab > Settings Button > Check for newer version of stored page" change that option to *Every Time I visit the webpage". That setting impacts how the webbrowser control deals with the refreshing.
Use the method refresh.
browser.Navigate("http://www.google.com") : browser.Refresh()