How to open a URL from MS Access with parameters
Asked Answered
S

3

12

I have a basic MS Access application that has a button on a form that should open a webpage with the ID of the Access record as the parameter, but everything I have tried results either in an error by Access or only the base URL opening in the web page.

I have tried adding VBA to the button's click event as so:

Application.FollowHyperlink _ 
"http://example.com/index.php?r=controller/action&id=" & Me.ID 

but all I get is the base URL opening on the web browser (ie http://example.com). If I remove the '?' and the '&' from the full URL the button will open the browser with the full URL minus the '?' and the '&', which of course errors the page.

I have tried setting a hyperlink control's property as:

="http://example.com/index.php?r=controller/action&id=" & Me.ID

but it does the same thing as noted above.

I have tried creating a Macro with the same results. I have tried using the Hyperlink Builder and using [formName]![id] as the parameter but same thing happens or Access errors.

I have read this article: https://msdn.microsoft.com/en-us/library/office/ff822080.aspx and tried adding the part in the URl after 'index.php/ to the ExtraInfo place in the code, but same thing.

Help! It can't be that hard to simply have Access open a URL with a parameter on the end of the URL.

Sev answered 7/11, 2016 at 9:51 Comment(2)
Can't you simply create a string like this: str_url = "http://example.com/index.php?r=controller/action&id=" & Me.ID and then open it from Application.FollowHyperlink? It should probably work.Benoit
It absolutely SHOULD be that simple, but this is Microsoft we're talking about and no that doesn't work. Thanks though.Sev
T
21

Application.FollowHyperlink is fickle.

Use either ShellExecute:
Open an html page in default browser with VBA?

or

CreateObject("Shell.Application").Open "http://example.com/index.php?r=controller/action&id=" & Me.ID 

see https://mcmap.net/q/356675/-how-can-excel-vba-open-file-using-default-application


If the URL is in a string variable, you may need to cast it to Variant, because that's what Shell.Application.Open expects:

strUrl = "http://example.com/index.php?r=controller/action&id=" & Me.ID
CreateObject("Shell.Application").Open CVar(strUrl)

see https://mcmap.net/q/356675/-how-can-excel-vba-open-file-using-default-application, thanks Toby and Anthony for pointing this out!

Townsman answered 7/11, 2016 at 10:26 Comment(2)
That works!!! Thank you so much! I simply added your code line above to the click event for the button and it opens just fine. Gotta love Microsoft simplicity <--sarcasm. Thanks again! I tried to upvote your answer but I don't have the rep to do it. I'm sorry. But that is absolutely the right answer.Sev
If you are having issues with this not working try combining it with @TobyOvod-Evertt suggestion as well.Vaginate
T
2

Note that if you are having issues with CreateObject("Shell.Application").Open not working with a variable, it might be a casting issue - try tossing a CVar() around the parameter. See https://mcmap.net/q/356675/-how-can-excel-vba-open-file-using-default-application for more details.

Taxdeductible answered 16/5, 2019 at 18:4 Comment(1)
I started to write off the accepted answer as not working until I tried this, thanks!Vaginate
I
0

This VBA opens Edge with the desired URL. See this page.

Private Sub lst_Click()
On Error GoTo ErrHandler
Dim varURL As Variant
    varURL = CVar(lst.Column(1, 0))
    If Len(varURL & "") > 0 Then
        CreateObject("Shell.Application").ShellExecute "microsoft-edge:" & varURL & """" ' open [expects a variant][2];
 End If
    GoTo CleanUp
ErrHandler:
    MsgBox Err.Description
    Resume CleanUp
    Resume 'debugging only
CleanUp:
End Sub
Istria answered 2/3, 2023 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.