How to open a URL from MS Access with parameters - vba

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.

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://stackoverflow.com/a/18922262/3820271
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://stackoverflow.com/a/56173911/3820271, thanks Toby and Anthony for pointing this out!

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://stackoverflow.com/a/56173911/8512931 for more details.

Related

VB macro changing encoding on REST request

I have an excel file with a button associated to a VB macro like this:
Sub button_macro()
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", "http://theurl/service?param=""Gestión"""
MyRequest.Send
End Sub
But the response I get is something like:
"Gestión" is not a valid value for param.
How can I avoid VB converting the 'ó' character to another encoding?
If I send the request via browser like:
http://theurl/service?param="Gestión"
The service answer as desired.
EDIT:
Curiously MsgBox "Código único" works as expected showing the 'ó' and 'ú' characters correctly.
It works in the browser because the browser encodes the request for you - you can see that if you use your browser's Developer tools - eg for your "example" URL of
http://theurl/service?param="Gestión"
I see
http://theurl/service?param=%22Gesti%C3%B3n%22
You can encode your parameters using (eg) the UTF-supporting method from the accepted answer here: How can I URL encode a string in Excel VBA?

Save a Form as a Report using VBA

In MS Access it's possible to convert a Form to a Report using the "Save Object As" functionality (Office Button -> Save As -> Save Object As "Save the current database object as a new object").
I'd like to have the same functionality using VBA but it seems to me that none of the following relevant DoCmd methods are suitable CopyObject, OutputTo, Save, TransferDatabase.
I was unable to find any useful information, except a blog post in which a code sample is provided using the SendKeys statement, but it seems not working.
Any advice?
It is not possible to convert a form to a report using VBA. Even using Sendkeys (which really should be avoided as there is a potential for the wrong application to get the keypresses instead) is not an effective method, as some controls don't get converted correctly.
Instead, you should be designing a report from scratch, with the required headers/sorting/grouping - it will look far better.
Regards,
Thanks to some tips from MajP on an access-programmers forum post, I found a way that is suitable for my needs, although it uses the sendkey function:
strFormName = "formname"
strReportName = "reportname"
DoCmd.SelectObject acForm, strFormName, True
SendKeys strReportName & "~"
RunCommand acCmdSaveAsReport
strFormName must contain the name of the Form to be saved as a Report whose name must be contained in strReportName.

How do I open a user's default browser open to the users homepage?

I am trying to figure out how to cause a Menu Strip item to open the active Windows accounts default browser to their homepage. I have tried Process.Start("about:blank") and for some reason this always opens Internet Explorer's about:blank page. (I have Google Chrome as my default browser with http://www.duckduckgo.com as its homepage on Windows 7 Pro.)
I know I can specify any URL to open the default browser, but how to get their selected homepage to open? I have found some articles based in C# that required looking into registry entries as to finding their chosen homepage per each browser. Would the process be the same/similar in VB.Net 2017 and how would I go about doing so? This is using VB.Net 2017 Community Edition and the project is a Windows.Forms desktop application.
The only way I found is to manually query the registry about the default command to handle the http protocol.
The first line of this code will return something like "C:\Program Files\Your Browser\browser.exe" -osint -url "%1", so you want to replace %1 by your landing page.
Then, if you want to use Process.Start with command line arguments, the first parameter will be the command and the second one the arguments.
Thus, we need to split the registry string between the command and the argument list. The regex will do this job.
I omited null checks and regex success for clarity.
Dim cmd = CStr(Registry.ClassesRoot.OpenSubKey("http\shell\open\command").GetValue(String.Empty))
cmd = cmd.Replace("%1","about:blank")
Dim r = new Regex("^""([^""]+)"" (.*)")
Dim m = r.Match(cmd)
Process.Start(m.Groups(1).Value, m.Groups(2).Value)
Found some clues here.
Dim readValue As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\
Associations\UrlAssociations\http\UserChoice", "Progid", Nothing).ToString
Will give an identifier for the current user's browser.
Dim path As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\"
& readValue & "\shell\open\command", "", Nothing).ToString
Will return the run command with path.
Add some code to extract the EXE and run it without arguments, for example;
Dim DivArr As Char() = {Chr(34), "\"c}
'split into segments using quotes and back-slash seperators
Dim parts() As String = path.Split(DivArr)
'find first segment with period/full-stop
Dim Executable As String = Array.Find(parts, Function(x) (x.Contains(".")))
Process.start(Executable)
You may try this:
Process.Start("your_url_here eg. www.homepage.com etc.")
and, this will open with google chrome if its your default browser.

CJWSoft ASP Classic Question Mark at End of URL after Log In

I've played with ASP classic and VB Script a few years since that's what we have at our company, but this is really bothering me. We bought the CJWSoft ASP Protect, and did some customizing, if anyone is familiar with it. Every page I log into ends with a question mark at the end of the URL and displays an internal server 500 error (using a testing server: Windows Web Server 2008 R2). One would think it would always go to the default.asp page upon login, but that doesn't happen unless I open a new window, which it's set to not store cookies. I can access any page I log into after I clear the question mark at the end of the URL. I have IT guys here, but we're not sure what's causing it.
(IP Address/Default.asp? [or] IP Address/password_admin/default.asp?) produces an
Internal Server Error 500.
Remove the ? and I'm into any of the pages on the server. Why?
I think the following script may have something to do with it, or whatever relates to it...
If Session("PasswordAccess") = "No" Then
Thispage = Request.ServerVariables("script_name")
Else
'Thispage = Request.ServerVariables("script_name") & "?" & Request.Querystring & Request.Form
'Setting Below is more secure than the setting above which allows form post data to be re-returned to the page
Thispage = Request.ServerVariables("script_name") & "?" & Request.Querystring
End If
Please help me resolve the question mark, anything else is a grand bonus!
What about only appending that ? if the query string isn't empty?
Like
<%
Thispage = Request.ServerVariables("script_name")
Dim qst : qst = Request.ServerVariables("QUERY_STRING")
if qst<>"" then Thispage = Thispage & "?" & qst
%>

Using vbscript to log on to internet provider webpage (data in html table)

I'm sure that this is a repeat post, but I've been unable to find exactly what I need. I currently live on a college campus where I need to enter a username and password to use the internet. I'm trying to automate the process (using .bat files) so I can run a server application on startup without ever pressing a key. Unfortunately, I keep getting an error at line 9 char 9: "Object does not support this property or method: 'getElementByID'" I tried replacing .getElementByID with .getElementByName, but it didn't make a difference.
Call Main
Function Main
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate "https://caserver.jbu.edu/auth/perfigo_weblogin.jsp"
Wait IE
With IE.Document
.getElementByID("username").value = "name"
.getElementByID("password").value = "password"
.getElementByID("tx_voputilities_pi1[sign_in]")(0).Submit
End With
End Function
Sub Wait(IE)
Do
WScript.Sleep 500
Loop While IE.ReadyState < 4 And IE.Busy
End Sub
I think that the problem has to do with the webpage. The "username" and "password" fields are elements inside a table. I couldn't find an example webpage with similar properties, so I'm at a loss. I'm not sure if the authentication page can be viewed from off-campus, so I attached a picture with some of the HTML (or at least the elements list)
My research so far:
VBScript to Launch a website login
IE 9 error getElementbyId: Object required
VBScript get contents of html table text input field
VBS website login script - "Object required" error
Like I said, I'm using batch files to start a computer. I wanted this to be a quick (and, yes, crude) solution, but there may be a better way to automate webpage logon. If there is a [quick] better way, feel free to point me in the correct direction.
Your problem may be that they don't use id, but name instead.
There is no function getElementByName, but there is getElementsByname
You could try
.getElementsByName("username")[0].value = "name"
.getElementsByName("password")[0].value = "password"
(I should probably note that this assumes the first element with these names is the one you are looking for - names do not have to be unique hence the array returned and the lack of a getElementByName function, whereas id is unique)
As a further note, your problem is not that the fields are in a table - getting an element from the DOM with either getElementById or getElementsByName do not take positioning into account, only that the element exists somewhere within the parent you are running the function on.