Auto Form

This code is a pseudo wizard, that allows you to automatically generate a HTML form from an SQL query. Additionally, it inserts the data contained in the form into the database. It is only necessary to indicate the the database reference, and the query that we want to execute.

<HTML><HEAD><META AUTHOR='Santi mondejar, [email protected]'></HEAD><body>
<% if len(Request.QueryString) = 0 then
'First call
Set db_cnx = Server.CreateObject("ADODB.Connection")
db_path="DBQ=" & server.mappath("db1.mdb")
Session("source")= db_path
db_cnx.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & Session("source")
Sql1 = " SELECT * FROM some_table "
Session("sql")=Sql1
Set db_rcdst1 = Server.CreateObject("ADODB.Recordset")
db_rcdst1.Open Session("sql"), db_cnx, 1, 2
%>
<form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="get" >
<table border>
<% for i=0  to db_rcdst1.fields.count-1 %>
<td><%=db_rcdst1(i).name %></TD><TD><input type=text name=<%=db_rcdst1(i).name %> size=<%=db_rcdst1(i).definedsize %>>
</TD><tr>
<%next%>
</tr><td colspan=2><input type="submit"></td></form></table>
<% Else
'Second call
Set db_cnx = Server.CreateObject("ADODB.Connection")
db_cnx.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & Session("source")
Set db_rcdst1 = Server.CreateObject("ADODB.Recordset")
db_rcdst1.Open Session("sql"), db_cnx, 1, 2
Set db_rcdst2 = Server.CreateObject("ADODB.Recordset")
db_rcdst2.Open Session("sql"), db_cnx, 1, 2
'starting database writing
db_rcdst1.AddNew                       
a=db_rcdst1.fields.count
    For Each Key In Request.QueryString 
a=a-1
        val_type=Request.QueryString(key)
        obj_width = db_rcdst2(a).definedsize
clase = db_rcdst2(a).type
Select Case clase rem VarType(db_rcdst2(a))
Case 2     
     val_type = Cint(val_type)
   Case 3 
     val_type = Clng(val_type)
Case 4   
     val_type = Csng(val_type)
   Case 5    
     val_type = Cdbl(val_type)
Case 6
     val_type = Ccur(val_type)
  Case 135
     If IsDate(val_type) Then
val_type = Cdate(val_type)
Else
val_type=Date
End If
Case  200  
var_type="Cstr"
val_type = Cstr(Left(val_type, obj_width))
  Case 11
    If UCase(val_type) = "true" Then
val_type = True
Else
val_type = False
End If
  Case 17     
    var_type="Cbyte"        
    val_type = Cbyte(val_type)
End Select
db_rcdst1(a)=val_type
Next
db_rcdst1.Update
db_rcdst1.Close  
db_rcdst2.Close        
    Set db_rcdst1 = Nothing  
    Set db_rcdst2 = Nothing
Session.Abandon
Response.Write("information have been registered")
finish=True
End If%></BODY></HTML>

You might also like...

Comments

Santi Mondejar

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”