Know the javascript - not sure how to convert to VBA...

Nightowl4933

Tryin' to do it right...
Local time
Today, 14:05
Joined
Apr 27, 2016
Messages
151
Hello,

I would like to open a web-based application from my Access project, but I'm not sure how to 'translate' the javascript to an Event Procedure on a Command Button.

The java script is:
="javascript:void(window.open('http://application/home/redirect/?explorer=false&m=premises&c=other&a=edit&i=" & Fields!PREMID.Value & "','_blank'))"

...and works from another web-based application.

I've got this far, which will open the program but not append the 'variables':

strApplication = "http://application/home/redirect/?explorer=false&m=premises&c=other&edit=&i=" & strPremID & "','_blank"

Application.FollowHyperlink strApplication

'strPremID' is the value of a text box on the same form as the Command Button, and I figured this should replace the 'Fields!PREMID.Value' - but perhaps that's where I'm going wrong?

Thanks,

Pete
 
Paste this code into a module, and it will open ANY file in its native application.

usage: OpenNativeApp "c:\folder\file.pdf"
will open it in acrobat
and
OpenNativeApp "http://applicationhomeredirect/...."
will open the doc in web browser, etc.

Code:
'Attribute VB_Name = "modNativeApp"
'Option Compare Database
Option Explicit
 Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
 Private Declare Function GetDesktopWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&
 
Public Sub OpenNativeApp(ByVal psDocName As String)
Dim r As Long, msg As String
 r = StartDoc(psDocName)
If r <= 32 Then
    'There was an error
    Select Case r
        Case SE_ERR_FNF
            msg = "File not found"
        Case SE_ERR_PNF
            msg = "Path not found"
        Case SE_ERR_ACCESSDENIED
            msg = "Access denied"
        Case SE_ERR_OOM
            msg = "Out of memory"
        Case SE_ERR_DLLNOTFOUND
            msg = "DLL not found"
        Case SE_ERR_SHARE
            msg = "A sharing violation occurred"
        Case SE_ERR_ASSOCINCOMPLETE
            msg = "Incomplete or invalid file association"
        Case SE_ERR_DDETIMEOUT
            msg = "DDE Time out"
        Case SE_ERR_DDEFAIL
            msg = "DDE transaction failed"
        Case SE_ERR_DDEBUSY
            msg = "DDE busy"
        Case SE_ERR_NOASSOC
            msg = "No association for file extension"
        Case ERROR_BAD_FORMAT
            msg = "Invalid EXE file or error in EXE image"
        Case Else
            msg = "Unknown error"
    End Select
'    MsgBox msg
End If
End Sub
 Private Function StartDoc(psDocName As String) As Long
Dim Scr_hDC As Long
 Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
End Function
 
Hi Ranman256,

I probably didn't give enough information, sorry.

The application I referred to isn't like Word, Excel, etc., but rather a database application that's written as a browser (forgive my terminology!), so what I'm trying to do is open that application in, I assume, the users default browser.

That bit works, but it's how I have to change the javascript to VBA to achieve the same outcome - opening the web application at the right record.

Unless I've completely misunderstood your code, of course..?

Pete
 
So, I've made some progress...

I've got the Strings all sorted and end up with the following variable:

strApplication = "http://application/home/redirect/?&target=_blank&m=premises&c=premises&a=edit&i=" & strPremID

This opens the application, but doesn't open the specific record. However, if I copy the string from the Immediates window (Debug.Print) and paste it directly in to the browser - it opens the record, too.

My founded is dumb!

Pete
 
Last edited:
Here is an example, GetWebPath is a stored path, the Exhibition is on the Form:

Code:
Function Web_ExhibitionsOriginals()

    With CodeContextObject
        Dim WebLink As String
        WebLink = GetWebPath & "Originals_Exhibitions.aspx?Exhibition=" & .[Exhibition] & "&OE=1" & ""
        Application.FollowHyperlink WebLink, , True
        HideWebToolBar
    End With
End Function

Simon
 
Hi Simon_MT,
This works exactly as you said it should - and when I run this with the same 'variables' with my code, the outcome is correct. It just won't open the browser at the appropriate record.

If I copy the string from the Immediates window and paste it into the web address of the browser, it goes to that record. So both methods give me the right string, but the browser won't get there!

I've even added a shortcut to my desktop with the string as the URL setting and it opens the correct record.

I'm a bit stumped...

Pete
 
Last edited:
Odd - it must be something in the way its being passed, for instance - this works fine;

sLink = "http://maps.google.com/maps?f=q&hl=en&q="
Application.FollowHyperlink sLink & Me.HomePostcode & " to " & Me.txtDest & "&layer=t"

It doesn't have some weird closing character after the record number, like a closing space or similar?
 
Hi Minty,

No, it doesn't. When I paste the resulting string in to IE address bar, it opens fine.

This is definitely a weird one!

Pete
 
...
If I copy the string from the Immediates window and paste it into the web address of the browser, it goes to that record. So both methods give me the right string, but the browser won't get there!
Only for error-finding, then I would take that string and use it as the link in the code, to see if that works.
 
Hi JHB,

I did that, too, and it still doesn't work.

Adding a shortcut on my desktop with that link works.
Pasting the link in to a browser works
Using the link in a On Click Event Procedure of a command button doesn't work, even if I do as you suggest.

I'll try and On Click event for another control, but I doubt that would make and difference...

Thanks,

Pete
 
Yeah, then you know it isn't something wrong with the values from the variables.
Does the browser open when the code runs?
Are you able to access other sites?
Could you show the code you use and also the string with the link?
I would examine the link string, character by character.
Something like below:
Code:
  For x = 1 To Len(YourLinkString)
    MsgBox (Asc(Mid(YourLinkString, x, 1)) & " " & Mid(YourLinkString, x, 1))
  Next x
 
Well, I have to say (whilst stuffing my face with Humble Pie), that ignoring the complicated and persevering with the easy doesn't always work - and it didn't this time.

Thanks to everyone for their help and suggestions, but Ranman256 had it - right off the bat!

I have no idea why the simple didn't work, but the Function does.

Thank you, Ranman256 ;) :D :o you did it again!

Pete
 

Users who are viewing this thread

Back
Top Bottom