How do you set controlsource to a link passed from openargs - ms-access

I'm building a help form which has a webbrowser control to display the help.html file. I'm using openargs from the calling form and what I've tried so far keeps coming up with the address is not valid. I know the file string works because I can type it directly into the webbrowser's control source on the property sheet and it works perfectly. I've spent about 3 hours trying to come up with different methods and the openargs seems the simplest if I could only get it to work.
Calling form:
DoCmd.OpenForm "frm_CS_Help", OpenArgs:="=""" & "C:\Users\bamad\Documents\CSHelp.html#DROPBOX"""
The Help form:
Private Sub Form_Load()
Dim strPath As String
strPath = Me.OpenArgs
WebBrowser43.ControlSource = strPath
The string that works when I type it directly on to the controlsource property for the webbrowser control is:
="C:\Users\bamad\Documents\CSHelp.html#DROPBOX"
Any help pointing me in the right direction would be much appreciated. Cheers

Related

Loop through every control in a form, with the form name defined as a variable

I am trying to create a basic search which looks for a partial keyword match of any control in a specified form. The form name is selected via combo box and is stored as a variable.
How do I use this to loop through the controls of the selected form?
I can easily loop through the controls of the current form with the following:
For Each ctrl In Me.Controls
Debug.Print ctrl.Name
Next ctrl
But I can't figure out how to reference an external form, with the variable essentially replacing Me.
I've tried using:
Dim ctrl as Control
Dim variableName as String
variableName = Me.cmboFormName
For each ctrl in Forms(variableName).Controls
Debug.Print ctrl.Name
Next ctrl
But this just returns error 438 (Object doesn't support this property or method).
You need to Dim ctrl as well, and you may have spaces in the form name:
Dim ctrl As Control
Dim variableName as String
variableName = "[" & Me.cmboFormName & "]"
For Each ctrl In Forms(variableName).Controls
Debug.Print ctrl.Name
Next
Try this:
For Each ctrl In UserForm1.Controls 'or use your form name.
Debug.Print ctrl.Name
Next ctrl
Since the error is occurring on Forms(variableName).Controls, the instruction is doing too many things to be easily debuggable. Split it up.
Declare a variable to contain a Form object - note, I'm no Access dev, I usually work with Excel and MSForms, so for me that would be a UserForm, but I very strongly suspect that your Access forms aren't from the MSForms type library (you'd have to work pretty hard to get a UserForm in Access), so I'm guessing the type to use is called Form, but I could be wrong - will be happy to edit if corrected in a comment.
Dim theForm As Form
Now Set the form object:
Set theForm = Forms(variableName)
If your code didn't blow up yet, then you've successfully retrieved a form instance. That wouldn't surprise me, because if that step failed you'd probably be facing a subscript out of range error instead.
If your library has a Controls collection class, declare a variable of that type:
Dim theControls As Controls
And assign it:
Set theControls = theForm.Controls
That could possibly blow up with run-time error 438, if Form doesn't have a Controls member... which wouldn't really add up, given Me.Controls seems to work.
So go back and declare theForm As Object, and let VBA's late-binding magic query the object's interface, instead of working with the specific Form interface - again I don't know much Access, but it's fairly possible that Me being a specific form type exposes a Controls collection, but not the general-purpose Form type - VBA UI framework internals are that kind of a mess.
So, to speak COM-gibberish, by declaring it as Object you allow the run-time to query IDispatch and locate the Controls member.
If you can get that instruction (the Controls collection assignment) to run without throwing, then you should be able to iterate its content without problems.
Also keep in mind that it seems the Forms collection only includes open forms.
Forms contains collection of opened forms. Unfortunately you cannot access a closed form using a variable name.
Just check whether the form is available. if not open the form either in nomal or design view and continue your code like this.
Dim ctrl as Control
Dim variableName as String
variableName = Me.cmboFormName
If Not (FN_FORM_ISLOADED(variableNAme)) Then
'Open your form in desired view.
DoCmd.OpenForm variableNAme, acNormal
End If
For each ctrl in Forms(variableName).Controls
Debug.Print ctrl.Name
Next ctrl
Place this function in your public module
Public Function FN_FORM_ISLOADED(iFormName As String) As Boolean
Dim I As Integer
FN_FORM_ISLOADED = False
For I = 0 To Forms.count - 1
If Forms(I).name = iFormName Then
FN_FORM_ISLOADED = True
Exit Function
End If
Next I
End Function

On Enter event not working inside list box control (Access 2007 vba)

I have a listbox control that displays search results. What I need done is when I hit the Enter key on any of the search result, I want a form opened up. The problem I am facing is, it works but I am required to hit the "Enter key" twice. Any idea why? I also tried placing the code in the "On Key Press" event, however for some reason, nothing happens.
Here is the code that I have
Private Sub SearchResults_Enter()
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmUpdateProc"
stLinkCriteria = "[ProcedureName] = '" & Me![SearchResults] & "'"
DoCmd.OpenForm stDocName
End Sub
Any help would be much appreciated.
Thanks
As I mentioned earlier, the Keydown event worked. Thank #pony2deer

Open VBA code of active form

I have an Access database with a huge amount of forms (300+) and VBA code. The backend of this Access db is in MS SQL.
When I am on a control in a form (in formview), I want to "jump" directly to the VBA code of this form without closing or putting the form in design mode. I can do this with a shortcut key which I assign to a function.
This works well when the control is not in a subform. But when this is the case, the code runs into an error telling me that the module of form cannot be found.
This is the code i use:
Dim sFrmName As String
sFrmName = Screen.ActiveControl.Parent.Name
If Nz(sFrmName, "") = "" Then Exit Function
'Open forms module
DoCmd.OpenModule "Form_" & sFrmName
How can i change this code so that i dont have to put the form in design mode to go to the VBA code of this form, which will also work for subforms? I know I can do this manually in VBE, but I'd like to do it in VBA.
Found one way to do it, this also works for subforms:
Dim sFrmName As String
sFrmName = Screen.ActiveControl.Parent.Name
If Nz(sFrmName, "") = "" Then Exit Function
'Open forms module
'DoCmd.OpenModule "Form_" & sFrmName
Application.VBE.ActiveVBProject.VBComponents("Form_" & sFrmName).CodeModule.CodePane.Show

Call method from another form

I have a navigation form with two subforms that looks something like this:
When the button in Subform A is clicked, I would like to call a method from Subform B. The method is defined like this:
Public Sub MyMethod()
Debug.Print "MyMethod called"
End Sub
I tried using Forms!SubformB.MyMethod but I get the error Database cannot find the referenced form 'SubformB'. Referring to this page, I also tried Forms!NavigationForm!SubformB.MyMethod but then I get Database cannot find the referenced form 'NavigationForm'. Does anyone know how to do this properly?
Thank you.
Function and Sub procedures defined in the class module of a Form are considered to be private to that form. If you want a Sub that can be called from several different forms then move that Sub to a "regular" VBA Module (i.e., one that is created when you choose Insert > Module from the menu bar in the VBA editor) and be sure to declare it as Public.
Here is a snippet of code I have that toggest the AllowEdits method of a subform when clicking a button on the mainform. Pretty sure this is what you are after.
Function ToggleEditStatus_Child(WhichForm, WhichChild As String)
'Changes the AllowEdits setting of the SubForm parameter
Dim Cform As Object
Set Cform = Forms(WhichForm).Controls(WhichChild).Form
Cform.AllowEdits = Not Cform.AllowEdits
Forms(WhichForm).Refresh
Cform.Refresh
Forms(WhichForm).Controls(WhichChild).SetFocus
Set Cform = Nothing
End Function
I did figure this out but there's a catch - the other form has to be open. This worked for me though so I'm posting the solution here.
First, navigate to the form:
DoCmd.BrowseTo ObjectType:=acBrowseToForm, _
ObjectName:="SubformB", _
PathToSubformControl:="NavigationForm.NavigationSubform", _
DataMode:=acFormEdit
Then make a pointer to the subform:
Dim f As Form_SubformB
Set f = Forms("NavigationForm").NavigationSubform.Form
And you are free to call the method:
f.MyMethod()
Note: you will most likely have to make the method Public for this to work.

An Access Form:How to get PDF of a access form

I have a Icon of PDF in my form that I have created in Access 2010. There are 3 tabs in that form; each tab have a separate form page and PDF icon is common for all the tabs.
Now I want that whenever a user click on that icon a PDF file of that form get created.
I have written this code
Private Sub cmdPrintReportPDF_Click()
Dim strDefaultPrinter As String
strDefaultPrinter = Application.Printer.DeviceName
**Set Application.Printer = Application.Printers("PDFCreator")**
'DoCmd.PrintOut acPrintAll
DoCmd.OpenReport "Graph_report", acViewNormal
Set Application.Printer = Application.Printers(strDefaultPrinter)
End Sub
But I'm getting the following error:
Invalid procedure call or argument on line no 4.
Set Application.Printer = Application.Printers("PDFCreator")
I have PDFCreator installed and this line, which triggers an error for you, does not trigger an error for me.
Set Application.Printer = Application.Printers("PDFCreator")
Go to the Immediate Window of the VB Editor and see if you also get an error with this line:
? Application.Printers("PDFCreator").DeviceName
If that also triggers an error, you probably don't have a printer whose DeviceName is PDFCreator. You can list the names of the printers with this procedure.
Public Sub ListPrinters()
Dim objPrinter As Printer
For Each objPrinter In Application.Printers
Debug.Print objPrinter.DeviceName
Next objPrinter
End Sub
However, with Access 2010, I think you can create a PDF without using your PDFCreator print device. At least this works for me with Access 2007, so I'm guessing it will work for you.
Private Sub cmdSaveAsPdf3_Click()
Dim strPath AS String
strPath = CurrentProject.Path & Chr(92) & "Sample3.pdf"
DoCmd.OutputTo acOutputForm, "fsubSample3", acFormatPDF, strPath
End Sub
That button click code creates a PDF (Sample3.pdf) of the form (fsubSample3) embedded in a page of my main form's tab control ... which is what I thought you wanted based on the original version of your question. Now it seems you're wanting to create a PDF of a report rather than a form. You can adapt the DoCmd.OutputTo line to use a report instead of a form.
Change this:
Set Application.Printer = Application.Printers("PDFCreator")
...to this:
Application.Printer = Application.Printers("PDFCreator")
Application.Printer is a property, not an object, so it doesn't use SET to change it.