VBScript runtime error '800a01a8', Object required: '' when using WScript.Quit - vbscript

I am putting the final pieces into a VBScript page and one thing I want to handle is if the variable being passed in via the URL is empty. If that is the case, I want the page to stop running and show a 'must log in' message.
I have the following code in place:
<%
If (Request.QueryString("nn") = "") Then
Response.Write "<p>You must be logged in to view content. <a href='http://URL/login?ReturnUrl=/interests'>Click here</a> to log in.</p>"
WScript.Quit
End If
%>
The error throws on the line WScript.Quit. Any ideas?

WScript.Quit, as the name implies, is part of the Windows Script Host. You appear to be using VBScript within a different host: ASP. If you need to stop processing an ASP page, just end the server's response by using:
Response.End

Related

How to get rid of prompt on quitting MS ACCESS

I am running a Sub procedure stored in an ACCESS database using VBScript. The procedure queries the database and exports/saves a CSV file.
My problem is that the script successfully opens the database, runs the procedure but then leaves ACCESS open because ACCESS opens a prompt asking "Are you sure that you want to leave ACCESS" (rather its equivalent in German). I want it to close without interaction.
The basic idea of this script is to run it via the Windows Task Scheduler. (Which does not work right now but that is another question.)
Here is the part of my VBScript dealing with the ACCESS database:
Dim objAccess
Set objAccess = createObject("Access.Application")
objAccess.OpenCurrentDataBase("C:\FiselTools\Allgemein\Scripte\Pellenc-CopyBelegarchiv3.accdb")
objAccess.Run "CopyBelegarchiv"
objAccess.Quit acQuitSaveAll
Set objAccess = Nothing
Using this script manually, it does open the database, export the file, finally executes the part following the code from above - so only closing ACCESS does not work and my guess is that it's not a problem with the script or the procedure but the ACCESS.
Erik A basically gave the right answer: there was some VBA code that is responsible for this prompt (see below). It was part of the primary form.
So in this case I found two possible solutions:
Do not display/show the form (which worked for me as I made a copy of the file dedicated for just this purpose).
If you need the form then you might need to delete the VBA code.
Private Sub Form_Unload(Cancel As Integer)
Dim Answer As Variant
Answer = MsgBox("Wollen Sie die Anwendung wirklich beenden?" & vbNewLine & vbNewLine & "Nicht gespeicherte Änderungen gehen dabei möglicherweise verloren", vbQuestion + vbYesNo + vbDefaultButton2)
If Answer = vbNo Then
Cancel = True
Else
DoCmd.Quit acQuitSaveNone
End If
End Sub
Since it is part of the Access class object of the form that is configured to be displayed on opening the ACCESS database and I do not need the form when I run the procedure via VBScript I simply changed the database to not display any form when the database is opened.
Some day I will remove any form, query, and any other object not needed from this special copy of the database that I don't need to run from VBScript.
Thanks!

FileExists Returning False

I'm creating a Classic ASP page and trying to determine if a .jpg is on my web server. If the image exists then I want to use it, but if the image does not exist, I display a generic image. This is always false. Any ideas?
<%dim fs
strFilePath =("http:/example.com/photos/" & PersonID & ".jpg")
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists(strFilePath) then
response.write "<img src='../photos/"& PersonID &".jpg'"&">"
else%>
<img src="http://exmaple.com/images/nophoto.jpg">
<%end if
set fs=nothing%>
The Scripting.FileSystemObject only supports accessing files from the file system and has no way to determine whether a file exists at a particular external URL. If the URL is within the current Web Application you can use;
Server.MapPath(relative_path)
which if passed a relative server path i.e "/photos" will return the physical path to the file on the server, which you can then test with fs.FileExists().
But if the URL is external you still have options. By using a server-side XHR request to the URL and based on the response, determine it's existence. We can also make this more efficient by only asking whether it is there and not returning the content, which we can do by using a HEAD request.
Here is an example of a possible implementation;
<%
Function CheckFileExists(url)
Dim xhr: Set xhr = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
With xhr
Call .Open("HEAD", url)
Call .Send()
CheckFileExists = (.Status = 200)
End With
End Function
If CheckFileExists("https://cdn.sstatic.net/Img/unified/sprites.svg?v=fcc0ea44ba27") Then
Call Response.Write("File Exists")
Else
Call Response.Write("File Doesn't Exist")
End If
%>
Output:
File Exists
Useful Links
Does File Exist? ASP Classic (Explains use of Server.MapPath())

Return to the previous page after executing response.write in VB scripting

I am trying to execute the following VB script where i have two reponse.redirect statement. My code works for the first response and then stays on that particular page. How can i execute the second response.redirect statement.
Here is my code -
<%
'Code to insert details into database
'At the end
Response.Redirect "/school/Subject/Course/Course.asp?courseCode=" &Course
Response.Redirect "/School/Subject/Calendar/Enroll.asp?id=" &id
%>
Please suggest me any way to execute both the lines. I tried windows.back() in the course.asp but it did not work.
You can not open two pages at same time.
This can be done using one by one like below
Pass querystring as IsEnroll=True
Response.Redirect "/school/Subject/Course/Course.asp?IsEnroll=True&courseCode=" &Course
In Course.asp Page
If Request.QueryString["IsEnroll"] !=null
{
Response.Redirect "/School/Subject/Calendar/Enroll.asp?id=" &id
}
This code is untested, but perhaps you can use (2) window.open scripts if you don't mind 2 additional browser window(s) being open. It'll pass your parameters through, and it'll leave you with 3 windows total, but it gets the job done.
Response.Write("<script>window.open('/school/Subject/Course/Course.asp?courseCode=" + Course + "','_blank');</script>");
Response.Write("<script>window.open('/School/Subject/Calendar/Enroll.asp?id=" + id + "','_blank');</script>");
Unless you edit Course.asp, and pass in querystring parameters for BOTH &Course, and &id. Then upon load pass &id from Course.asp to Enroll.asp. It'd take some significant redesign, and you'd need access to the source code.
Haven't seen much Classic ASP these days, sounds/looks like a Legacy System.

Will this VBSCRIPT code trigger the message box?

I'm looking through at an old Classic ASP page that uses VBScript. The code below appears to use a variable stored in the config (global.asa) called called CODES_TIMESTAMP. However looking at the servers in question it appears that that variable no longer exists. My question is, if that variable is not defined in the config file then will the error message box be activated?
Dim DB_TIMESTAMP_CODES
DB_TIMESTAMP_CODES = "<%=Application("CODES_TIMESTAMP")%>"
If trim(DB_TIMESTAMP_CODES) = "" Then
msgbox "Setup Error... Codes are not Defined"
End If
My question is, if that variable is not defined in the config file then will the error message box be activated?
Value will be "". But msgbox cannot be executed on ASP web page. msgbox will appear only from VBS script.
The code below appears to use a variable stored in the config (global.asa) called called CODES_TIMESTAMP
You (previous developer) could assign value to Application variable from any page. I suggest you to make full search over all .ASP pages, may be this value assigned not in GLOBAL.ASA

Vb.net not closing file after reading from it

When refreshing my application, I have to read from a text file which has information stored in it. After it is read I make sure I have closed it 'file.close()' the next time I refresh the applicaztion the text file should be generated again and overright the previous one.
The problem is that it cant change the file because it is aparantly in use by another process but as soon as I close my application it works fine. Is there anyway to stop the process before refreshing it so it works correctly? The text file is called NetworkInfo.txt.
Thanks
Chris
EDIT
Every time I refresh the application I run a batch file that generates a file with all the network info in. (IPConfig/all)
I then have a module that reads from it like the following:
Public Function EthDefaultGateway() As String
Dim sr As New System.IO.StreamReader(ipconfig)
Try
Dim foundEthernet As Boolean = False
Dim gateway As String = ""
Do Until sr.EndOfStream
Dim line As String = sr.ReadLine()
If line.Contains("Ethernet adapter LAN:") OrElse line.Contains("Ethernet adapter Local Area Connection:") Then
foundEthernet = True
End If
If foundEthernet Then
If line.Contains("Default Gateway . . . . . . . . . :") Then
gateway = line.Substring(line.IndexOf(":") + 1).Trim
Exit Do
End If
End If
Loop
If gateway = "" Then
gateway = "Unknown"
End If
If gateway = "::" Then
gateway = "Unknown"
End If
Return gateway
Catch ex As Exception
EthDefaultGateway = "Unknown"
End Try
sr.Close()
sr.Dispose()
End Function
From this I gather all the bits of information I need. (There probs is a lot better way of doing this but Im only a nooby and I cant find any other ideas on here, the web or from friends.)
All of these close the file after readinf from it (sr.close)
Some reason though its not closing it. The only other thing It could be is the batch file isnt closing it which I think is very unlikely
The problem is that when I change the IP or refresh the form it fails because the batch file cannot over write the network info file.
Any suggestions?
I was just thinking of a way to kill the process after the refresh but I didnt know if this was a good idea or if it was doable or how to do it tbh.
Return gateway
That bypasses the sr.Close() call at the bottom. So the file won't be closed. Always favor using the Using statement so it is automatic and can't be forgotten or skipped or bypassed because of an exception:
Public Function EthDefaultGateway() As String
Using sr As New System.IO.StreamReader(ipconfig)
'' Rest of your code
End Using
End Function

Resources