0% found this document useful (0 votes)
251 views

Vbscript

The document discusses various VBScript concepts and code snippets including: 1. A FUNCTION that defines a counter function that takes two parameters, performs addition and multiplication, and returns a string with the results. 2. Code showing how to use the BeginTrans and CommitTrans methods to start and commit a database transaction along with error handling to rollback if needed. 3. An example of splitting a string into an array using the Split function and then looping through the array. 4. Examples of using Session variables to store and retrieve data, retrieving server variables, and accessing fields in a Recordset object. 5. An SQL UPDATE statement example and error handling code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
251 views

Vbscript

The document discusses various VBScript concepts and code snippets including: 1. A FUNCTION that defines a counter function that takes two parameters, performs addition and multiplication, and returns a string with the results. 2. Code showing how to use the BeginTrans and CommitTrans methods to start and commit a database transaction along with error handling to rollback if needed. 3. An example of splitting a string into an array using the Split function and then looping through the array. 4. Examples of using Session variables to store and retrieve data, retrieving server variables, and accessing fields in a Recordset object. 5. An SQL UPDATE statement example and error handling code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Vbscript FUNCTION

Function counter(x,y)
dim sum, prod
sum = x + y
prod = x * y
counter = "the sum is: " & sum & vbnewline & "and the product is: "
& prod
End function

The function name "counter" would then be misleading, though, would it not?

Vbscript BeginTrans/commit
'The next line starts a transaction
adoConn.BeginTrans
Set adoCmd = Server.CreateObject("ADODB.Command")
'I added this to give the command 45 seconds to execute.
adoCmd.CommandTimeout = 45
adoCmd.ActiveConnection = adoConn
adoCmd.CommandText = "sproc_RecalculateQuantities"
adoCmd.CommandType = adCmdStoredProc
adoCmd.Execute
Set colErrs=adoConn.Errors
If adoConn.Errors.Count <> 0 then
For Each objError In colErrs
'This is the error number for a timeout.
If objError.Number=-2147217871 Then
adoConn.RollbackTrans
Response.Write "The query timed out before finishing. Please try again."
timeout=True
adoConn.Errors.Clear
Exit For
End If
Next
End If
Set adoCmd = Nothing

If Not timeout Then


adoConn.CommitTrans
End If
adoConn.Close
Set adoConn = Nothing

Conn.BeginTrans ' Begin transaction.


SQL = "Delete * from Faculty where " & _
      "FacultyID = '100000000'"
Conn.Execute SQL, nRecords
TotalErrors = Conn.Errors.Count
SQL = "Delete * from FacultyReference " & _
      "where FacultyID = '100000000'"
Conn.Execute SQL, nRecords
TotalErrors = TotalErrors + Conn.Errors.Count

If TotalErrors = o Then
   Conn.CommitTrans
Else
   Conn.RollbackTrans
End If

Vbscript Checkbox array/split


choices = Request.Form("computer")
choices = Split(choices, ",")
For Each member in choices
Response.Write member & "<BR>"
Next

VBscript Session Variables


Session("UserName") = Response.Form("UserName")
<%=Session("UserName")%>

VBscript Header Request


<%= Request.ServerVariables("HTTP_PROVIDERID") %>

VBscript Recordset Variables


<%=(test2.Fields.Item("dv_prov_id").Value)%>

SQL Update Statement


UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'

UPDATE dbo…
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'

Error Handling
Err.Description (gives a description of the error) Err.Number (the error code that was
associated with the error) Err.Raise (initiates an error code) Err.Clear (clears the values of
the Err object – resetting all values)

------------------- Begin Code Snippet ------------------- On Error Resume Next

' Regular code here....


Err.Raise(7) 'Simulate and "out of memory" Error 'Check to see if any errors have occurred. If
Err.Number <> 0 Then 'Capture, display, and clear error Wscript.Echo Computer & " " &
Err.Description Err.Clear Else WScript.Echo "No errors encountered." End If--------------------
End Code Snippet --------------------

<%
else
sql="DELETE FROM customers"
sql=sql & " WHERE customersID='" & cid & "'"
on error resume next
conn.Execute sql
if err<>0 then
response.write("No update permissions!")
else
response.write("Record " & cid & " was deleted!")
end if
end if
conn.close
%>

You might also like