Code:
<%
'*************************************************
' Form builder
' Created by: Derek Robertson
' Last modified: 12/01/2009
'
'*************************************************
Dim strAction, strSQL, rsBuilder, strTable, strIDField, adoConn, strActiveConn
'################################ FUNCTIONS AT END OF PAGE
strAction = Request("action")
strTable = Request("table")
Dim strFormCollection : strFormCollection = ""
Dim strHTMLForm : strHTMLForm = ""
Dim strInsertQuery : strInsertQuery = ""
Dim strUpdateQuery : strUpdateQuery = vbtab&"strSQL = ""Update " & strTable & " Set ""&_"&vbcrlf
Dim strSelectQuery : strSelectQuery = ""
Dim strListQuery : strListQuery = ""
Dim strListHeader : strListHeader = ""
Dim strDeclaration : strDeclaration = ""
Dim strFields : strFields = ""
Dim strValues : strValues = ""
Dim strDeleteQuery : strDeleteQuery = ""
Dim intRowCount : intRowCount = 2
strActiveConn = DatabaseConnection()
If strAction = "1" Then
call OpenDB()
strSQL = "select column_name, ordinal_position, data_type, character_maximum_length from information_schema.columns where table_name = '" & strTable & "' order by ordinal_position"
Set rsBuilder = adoConn.Execute(strSQL)
If Not rsBuilder.EOF Then
strDeleteQuery = vbtab & "strSQL = ""Delete From " & strTable & " Where " & rsBuilder("column_name") & " = "" & intRecordID"&vbcrlf&vbtab&"OpenDB()"&vbcrlf&vbtab&"adoConn.Execute(strSQL)"&vbcrlf&vbtab&"CloseDB()"&vbcrlf
strIDField = rsBuilder("column_name")
strListHeader = strListHeader & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>"
strListQuery = strListQuery & "<td><a href=""& request.ServerVariables(""SCRIPT_NAME"")&""?formGo=3&id=""& rsResults("""&rsBuilder("column_name")&""")&"">""&rsResults("""&rsBuilder("column_name")&""")&""</a></td>"
rsBuilder.MoveNext
Do Until rsBuilder.EOF
Select Case rsBuilder("data_type")
Case "int", "smallint", "bigint", "tinyint"
strDeclaration = strDeclaration & "int"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "int"&rsBuilder("column_name")&" = Numeric(Request.Form("""&rsBuilder("column_name")&"""))" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "int"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td><input name="""&rsBuilder("column_name")&""" type=""text"" value=""<"&"%= int"&rsBuilder("column_name")&"%"&">"" maxlength=""5""></td>" &vbcrlf &_
vbtab &"</tr>" & vbcrlf
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & """&int"&rsBuilder("column_name")&"&"","
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = ""&int"&rsBuilder("column_name")&"&"",""&_"&vbcrlf
Case "nvarchar", "varchar"
strDeclaration = strDeclaration & "str"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "str"&rsBuilder("column_name")&" = FormatEncode(Request.Form("""&rsBuilder("column_name")&"""))" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "str"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td><input name="""&rsBuilder("column_name")&""" type=""text"" value=""<"&"%= str"&rsBuilder("column_name")&"%"&">"" maxlength="""&rsBuilder("character_maximum_length")&"""></td>" &vbcrlf &_
vbtab &"</tr>" & vbcrlf
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & "'""&str"&rsBuilder("column_name")&"&""',"
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = '""&str"&rsBuilder("column_name")&"&""',""&_"&vbcrlf
Case "ntext", "text"
strDeclaration = strDeclaration & "str"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "str"&rsBuilder("column_name")&" = FormatEncode(Request.Form("""&rsBuilder("column_name")&"""))" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "str"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td><textarea name="""&rsBuilder("column_name")&""" type=""text""><"&"%= str"&rsBuilder("column_name")&"%"&"></text "&"area></td>" &vbcrlf &_
vbtab &"</tr>" & vbcrlf
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & "'""&str"&rsBuilder("column_name")&"&""',"
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = '""&str"&rsBuilder("column_name")&"&""',""&_"&vbcrlf
Case "bit"
strDeclaration = strDeclaration & "bln"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "bln"&rsBuilder("column_name")&" = Request.Form("""&rsBuilder("column_name")&""")" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "bln"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td><input name="""&rsBuilder("column_name")&""" type=""checkbox"" value=""1""></td>" &vbcrlf &_
vbtab &"</tr>" & vbcrlf
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & "'""&bln"&rsBuilder("column_name")&"&""',"
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = '""&bln"&rsBuilder("column_name")&"&""',""&_"&vbcrlf
Case "smalldatetime", "datetime"
strDeclaration = strDeclaration & "dte"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "If IsDate(Request.Form("""&rsBuilder("column_name")&""")) Then dte"&rsBuilder("column_name")&" = MediumDate(Request.Form("""&rsBuilder("column_name")&"""))" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "dte"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td>"&DateDropDown(rsBuilder("column_name"))&"</td>" &vbcrlf &_
vbtab &"</tr>" & vbcrlf
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & "'""&dte"&rsBuilder("column_name")&"&""',"
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = '""&dte"&rsBuilder("column_name")&"&""',""&_"&vbcrlf
Case "money"
strDeclaration = strDeclaration & "dec"&rsBuilder("column_name")&", "
strFormCollection = strFormCollection & vbtab & "dec"&rsBuilder("column_name")&" = Request.Form("""&rsBuilder("column_name")&""")" & vbcrlf
strSelectQuery = strSelectQuery & vbtab & "dec"&rsBuilder("column_name")&" = rsResults("""&rsBuilder("column_name")&""")" & vbcrlf
strHTMLForm = strHTMLForm & vbtab &"<tr>" & vbcrlf &_
vbtab & vbtab & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>" &vbcrlf &_
vbtab & vbtab & "<td><input name="""&rsBuilder("column_name")&""" type=""text"" value=""<"&"%= dte"&rsBuilder("column_name")&"%"&">"" maxlength="""&rsBuilder("character_maximum_length")&"""></td>" &vbcrlf &_
vbtab &"</tr>"
strFields = strFields & rsBuilder("column_name") & ","
strValues = strValues & """&dec"&rsBuilder("column_name")&"&"","
strUpdateQuery = strUpdateQuery & vbtab & vbtab & """" & rsBuilder("column_name") & " = ""&dec"&rsBuilder("column_name")&"&"",""&_"&vbcrlf
End Select
strListHeader = strListHeader & "<td>"&Replace(rsBuilder("column_name"), "_", " ")&"</td>"
strListQuery = strListQuery & "<td>""&rsResults("""&rsBuilder("column_name")&""")&""</td>"
intRowCount = intRowCount + 1
rsBuilder.MoveNext
Loop
strListHeader = strListHeader & "<td>Delete</td>"
strListQuery = strListQuery & "<td><a href=""& request.ServerVariables(""SCRIPT_NAME"")&""?formGo=2&id=""& rsResults(0)&"">Delete</a></td>"
strDeclaration = "Dim "& Left(strDeclaration,len(strDeclaration)-2) & ", intRecordID, arrDate, intCounter, strSQL, rsResults"
strHTMLForm = "%"&"><form name=""form1"" action=""<"&"%=Request.ServerVariables(""SCRIPT_NAME"")%"&">?id=<"&"%=intRecordID%"&">"" method=""post"" style=""margin:0px;"">"& vbcrlf&_
"<table>"& vbcrlf&_
strHTMLForm& vbcrlf&_
"<tr>" & vbcrlf&_
vbtab & "<td>Submit</td>" & vbcrlf&_
vbtab & "<td><input type=""button"" value=""Cancel"" name=""Cancel"" onClick=""history.go(-1);"" /> <input name=""submit"" type=""submit"" value=""Update""><input name=""formGo"" type=""hidden"" value=""1"" />" & vbcrlf&_
vbtab & "</td>" & vbcrlf&_
"</tr>"& vbcrlf&_
"</table>"& vbcrlf&_
"</form><"&"%"
strFormCollection = "Function GetFormValues()" & vbcrlf &_
strFormCollection &_
"End Function"
strInsertQuery = "Function InsertIntoDatabase()" & vbcrlf &_
vbtab & "strSQL = ""Insert Into " & strTable & " (" & Left(strFields,len(strFields)-1) & ") Values(" & Left(strValues,len(strValues)-1) & ")"""&vbcrlf&_
vbtab & "OpenDB()"&vbcrlf&_
vbtab & "adoConn.Execute(strSQL)"&vbcrlf&_
vbtab & "CloseDB()"&vbcrlf&_
"End Function"
strUpdateQuery = "Function UpdateDatabase(intRecordID)" & vbcrlf &_
Left(strUpdateQuery,len(strUpdateQuery)-6) & """&_"& vbcrlf & vbtab & vbtab & """" & " Where " & strIDField & " = "" & intRecordID"&vbcrlf&_
vbtab&"OpenDB()"&vbcrlf&vbtab&"adoConn.Execute(strSQL)"&vbcrlf&vbtab&"CloseDB()"&vbcrlf&_
"End Function"
strSelectQuery = "Function GetDBValues(intRecordID)" & vbcrlf &_
vbtab&"strSQL = ""select * from "&strTable&" where "&strIDField&" = "" & intRecordID"&vbcrlf&_
vbtab&"OpenDB()"&vbcrlf&_
vbtab&"Set rsResults=adoConn.Execute(strSQL)"&vbcrlf&_
strSelectQuery & vbcrlf &_
vbtab&"rsResults.Close()"&vbcrlf&_
vbtab&"Set rsResults = Nothing"&vbcrlf&_
vbtab&"CloseDB()"&vbcrlf&_
"End Function"
strListQuery = "Function ListRows()" & vbcrlf &_
vbtab&"strSQL = ""select * from "&strTable&""""&vbcrlf&_
vbtab&"OpenDB()"&vbcrlf&_
vbtab&"Set rsResults=adoConn.Execute(strSQL)"&vbcrlf&_
vbtab&"Response.write(""<a href=""""""&Request.ServerVariables(""SCRIPT_NAME"")&""?formGo=3"""">New Record</a><br />"")&vbcrlf"&vbcrlf&vbtab&_
vbtab&"Response.write(""<table width=""""100%"""" cellpadding=""""0"""" cellspacing=""""0"""">"")&vbcrlf"&vbcrlf&vbtab&_
vbtab&"Response.write(""<tr>"")&vbcrlf"&vbcrlf&vbtab&vbtab&_
vbtab&"Response.write("""&strListHeader&""")&vbcrlf" & vbcrlf&vbtab&vbtab &_
vbtab&"Response.write(""</tr>"")&vbcrlf"&vbcrlf&vbtab&_
vbtab&"If Not rsResults.EOF Then"&vbcrlf&vbtab&_
vbtab&"Do Until rsResults.EOF"&vbcrlf&vbtab&vbtab&_
vbtab&"Response.write(""<tr>"")&vbcrlf"&vbcrlf&vbtab&vbtab&_
vbtab&"Response.write("""&strListQuery&""")&vbcrlf" & vbcrlf&vbtab&vbtab &_
vbtab&"Response.write(""</tr>"")&vbcrlf"&vbcrlf&vbtab&_
vbtab&"rsResults.MoveNext"&vbcrlf&vbtab&_
vbtab&"Loop"&vbcrlf&vbtab&_
vbtab&"Else"&vbcrlf&_
vbtab&"Response.write(""<tr><td colspan="""""&intRowCount&""""">No Results</td>"")&vbcrlf"&vbcrlf&vbtab&vbtab&_
vbtab&"End If"&vbcrlf&_
vbtab&"Response.write(""</table>"")&vbcrlf"&vbcrlf&vbtab&_
vbtab&"rsResults.Close()"&vbcrlf&_
vbtab&"Set rsResults = Nothing"&vbcrlf&_
vbtab&"CloseDB()"&vbcrlf&_
"End Function"
strDeleteQuery = "Function DeleteRow(intRecordID)" & vbcrlf &_
strDeleteQuery & vbcrlf &_
"End Function"
End If
rsBuilder.close()
Set rsBuilder = Nothing
call CloseDB()
End If
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Dereks form builder</title>
<script language="javascript" type="text/javascript">
function ReplaceAll() {
var strReplaceAll = document.results.text.value;
var intIndexOfMatch = strReplaceAll.indexOf( "</text area>" );
// Loop over the string value replacing out each matching
// substring.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strReplaceAll = strReplaceAll.replace( "</text area>", "</textarea>" )
// Get the index of any next matching substring.
intIndexOfMatch = strReplaceAll.indexOf( "</text area>" );
}
document.results.text.value = strReplaceAll
}
</script>
</head>
<body<%If strAction = "1" Then%> onload="ReplaceAll()"<%End If%>>
<form action="formbuilder.asp" method="post" name="sqltool">
<table width="100%">
<tr>
<td valign="top">
table name: <input type="text" name="table" value="<%=strTable%>" style="width:200px;" />
<input type="hidden" name="action" value="1" />
<input type="submit" />
</td>
</tr>
</table>
</form>
<%If strAction = "1" Then%>
<form action="formbuilder.asp" method="post" name="results">
<textarea name="text" style="width:1250px;height:800px;">
<%
Response.write("<"&"%option explicit%"&">"&vbcrlf)
Response.write("<"&"!--#include virtual=""/inc/connfunc.asp""--"&">"&vbcrlf)
Response.write("<"&"!--#include virtual=""/inc/functions.asp""--"&">"&vbcrlf)
Response.write("<"&"%"&vbcrlf)
Response.write(strDeclaration&vbcrlf)
Response.write("%"&">"&vbcrlf)
Response.write("<html>"&vbcrlf)
Response.write("<head>"&vbcrlf)
Response.write("</head>"&vbcrlf)
Response.write("<body>"&vbcrlf)
Response.write("<"&"%"&vbcrlf)
Response.write(ActionLogic())
Response.write("%"&">"&vbcrlf)
Response.write("</body>"&vbcrlf)
Response.write("</html>"&vbcrlf)
Response.write("<"&"%"&vbcrlf)
Response.write(strFormCollection&vbcrlf)
Response.write(vbcrlf)
Response.write(strListQuery&vbcrlf)
Response.write(vbcrlf)
Response.write(strInsertQuery&vbcrlf)
Response.write(vbcrlf)
Response.write(strUpdateQuery&vbcrlf)
Response.write(vbcrlf)
Response.write(strSelectQuery&vbcrlf)
Response.write(vbcrlf)
Response.write(strDeleteQuery&vbcrlf)
Response.write("%"&">"&vbcrlf)
%>
</textarea>
</form>
<%End If%>
</body>
</html>
<%
Function DateDropDown(strFieldName)
DateDropDown = vbcrlf&"<"&"%"&vbcrlf&_
"If dte"&strFieldName&" <> """" Then"&vbcrlf&_
vbtab&"arrDate = Split(dte"&strFieldName&",""/"")"&vbcrlf&_
"Else"&vbcrlf&_
vbtab&"arrDate = Split(""0/0/0"",""/"")"&vbcrlf&_
"End If"&vbcrlf&_
"%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&"<select name="""&strFieldName&""" id="""&strFieldName&"1"">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""0"">?</option>"&vbcrlf&_
"<"&"%for intCounter = 1 to 31%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""<"&"%=intCounter%"&">""<"&"%if intCounter = Cint(arrDate(0)) Then Response.write("" selected=""""selected"""""")%"&">><"&"%=intCounter%"&"></option>"&vbcrlf&_
"<"&"%next%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&"</select>"&vbcrlf&_
vbtab&vbtab&vbtab&"<select name="""&strFieldName&""" id="""&strFieldName&"2"">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""0"">?</option>"&vbcrlf&_
"<"&"%for intCounter = 1 to 12%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""<"&"%=intCounter%"&">""<"&"%if intCounter = Cint(arrDate(1)) Then Response.write("" selected=""""selected"""""")%"&">><"&"%=monthname(intCounter)%"&"></option>"&vbcrlf&_
"<"&"%next%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&"</select>"&vbcrlf&_
vbtab&vbtab&vbtab&"<select name="""&strFieldName&""" id="""&strFieldName&"3"">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""0"">?</option>"&vbcrlf&_
"<"&"%for intCounter = Cint(Year(NOW)) to Cint(Year(NOW))+7%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&vbtab&"<option value=""<"&"%=intCounter%"&">""<"&"%if intCounter = Clng(Left(arrDate(2),4)) Then Response.write("" selected=""""selected"""""")%"&">><"&"%=intCounter%"&"></option>"&vbcrlf&_
"<"&"%next%"&">"&vbcrlf&_
vbtab&vbtab&vbtab&"</select>"&vbcrlf&vbtab&vbtab
End Function
Function ActionLogic()
ActionLogic = "Session.lcid = 3081"&vbcrlf&_
"intRecordID = Request(""id"")"&vbcrlf&_
"Select Case Request(""formGo"")"&vbcrlf&_
" Case 1"&vbcrlf&_
" GetFormValues()"&vbcrlf&_
" If intRecordID <> """" Then"&vbcrlf&_
" UpdateDatabase(intRecordID)"&vbcrlf&_
" response.redirect(Request.ServerVariables(""SCRIPT_NAME""))"&vbcrlf&_
" Else"&vbcrlf&_
" InsertIntoDatabase()"&vbcrlf&_
" response.redirect(Request.ServerVariables(""SCRIPT_NAME""))"&vbcrlf&_
" End If"&vbcrlf&_
" Case 2"&vbcrlf&_
" If intRecordID <> """" Then DeleteRow(intRecordID)"&vbcrlf&_
" response.redirect(Request.ServerVariables(""SCRIPT_NAME""))"&vbcrlf&_
" Case 3"&vbcrlf&_
" If intRecordID <> """" Then GetDBValues(intRecordID)"&vbcrlf&_
strHTMLForm&vbcrlf&_
" Case Else"&vbcrlf&_
" ListRows()"&vbcrlf&_
"End Select"
End Function
Private Function DatabaseConnection()
Dim dbsUrl, dbsName, dbsUser, dbsPword
dbsUrl = "123.456.789.098"
dbsName = "northwind"
dbsUser = "fred_bloggs"
dbsPword = "password"
DatabaseConnection = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=" & dbsUrl & ";Initial Catalog=" & dbsName & ";User Id=" & dbsUser & ";Password=" & dbsPword & ";"
End Function
'Open the connection
Sub OpenDB()
set adoConn = Server.CreateObject("ADODB.Connection")
adoConn.Open strActiveConn 'stick your conn string here
End Sub
'Close the connection
Sub CloseDB()
adoConn.Close()
Set adoConn = Nothing
End Sub
%>
Bookmarks