Generate a data form from any table


Usage:

Call this sub:

call display_table("users","user_id","first_name,last_name.city")
"users" is the name of the table you want to display, the second value is a list of fileds you do not want to display (seperated by commas). You must add your autogenerated primary key here(if you have one). The last field is used for requesred fields.

The display_table procedure will also call the show_records sub, which will display all the data in the table.

I am using a MySql db here but it can run on any DB.

You have to fill in the CAPITALIZED words.

If you have any questions regarding this code please feel free to e-mail me:
aboud12@gmail.com

Watch this script in action!


Code:

PAGE_NAME.asp
<%
set cndb = server.CreateObject("adodb.connection")
cndb.Open "DRIVER={MySQL ODBC 3.51 Driver}; Server=DB_SERVER; Port=3306; Option=16387; Database=DB_NAME; Uid=USER_NAME; Pwd=PASSWORD;"
call display_table("users","user_id","first_name,last_name,city")

sub display_table(table_name, no_list,reqfields)
dim edit_type
edit_type = "ADD"
set rs = server.CreateObject("adodb.recordset")
sql = "select * from " & table_name
if request("edit_list") <> "" then
sql = sql & " where 1=1 and " & replace(replace(check_db(request("edit_list")),",", " and "),"@-$","'")
edit_type = "EDIT"
end if
'response.Write(sql)
rs.open sql, cndb
' create the add / edit table
dont_list_this_field = 1
%>
<table bgcolor="#000066" cellpadding="5" cellspacing="1" border="0">
<script>
function onlynumbers(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
alert('You can only enter numbers here');
return false;
}
return true;
}
function reqfields(val)
{
var reqfields = "<%=reqfields%>"
var mySplitResult = reqfields.split(",");
for(i = 0; i < mySplitResult.length; i++)
{
if(document.getElementById(mySplitResult[i]).value=="")
{
alert("Please fill in " + mySplitResult[i].replace('_',' '));
document.getElementById(mySplitResult[i]).focus();
return false;
}
}
}
</script>
<form method="post" action="">
<% for i = 0 to rs.fields.count - 1
' dont show non-listed records
arr_nolist = split(no_list,",")
for lst = 0 to ubound(arr_nolist)
if ucase(arr_nolist(lst)) = ucase(rs.fields(i).name) then dont_list_this_field = 0
next
if dont_list_this_field <> 0 then
if request("edit_list") <> "" and not rs.eof then curval = rs.fields(i).value
%>
<tr>
<td bgcolor="#FFFFFF"><%=format_ucase(replace(rs.fields(i).name,"_"," "))%></td>
<td bgcolor="#FFFFFF">
<% if rs.fields(i).type = 201 then %>
<textarea name="<%=rs.fields(i).name%>" id="<%=rs.fields(i).name%>"><%=curval%></textarea>
<% else %>
<% if rs.fields(i).type = 3 then %>
<input type="text" id="<%=rs.fields(i).name%>" name="<%=rs.fields(i).name%>" value="<%=curval%>" onkeypress="return(onlynumbers(event))">
<% else %>
<input type="text" id="<%=rs.fields(i).name%>" name="<%=rs.fields(i).name%>" value="<%=curval%>">
<% end if %>
<% end if %>
</td>
</tr>
<%
end if
dont_list_this_field = 1
next
%>
<tr>
<td bgcolor="#FFFFFF" colspan="2" align="center">
<input type="submit" name="<%=edit_type%>" value="<%=edit_type%>" style="border:solid #000066 1px;" onClick="return(reqfields())">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<% if edit_type = "EDIT" then %>
<input type="hidden" name="delete_id" value="<%=request.form("db_search")%>">
<input type="submit" name="DELETE"
value="DELETE" style="border:solid #000066 1px;" onClick="return(confirm('Are you sure you want to delete this record?'))">
<% end if %>
</td>
</tr>
</form>
</table>
<%
if request.form("ADD") <> "" Then
for i = 0 to rs.fields.count - 1
for lst = 0 to ubound(arr_nolist)
if ucase(arr_nolist(lst)) = ucase(rs.fields(i).name) then dont_list_this_field = 0
next
if dont_list_this_field <> 0 then
db_fields = db_fields & "," & rs.fields(i).name
'check for integer and make db compatible
if rs.fields(i).type = 3 then
if request(rs.fields(i).name) <> "" then
db_values = db_values & "," & check_db(request(rs.fields(i).name))
else
db_values = db_values & ",NULL"
end if
else
db_values = db_values & ",'" & check_db(request(rs.fields(i).name)) & "'"
end if
end if
dont_list_this_field = 1
next
sql = "insert into " & table_name & "(" & mid(db_fields,2) & ") values (" & mid(db_values,2) & ")"
response.Write "new record added."
cndb.execute sql
response.Redirect(request.ServerVariables("url"))
end if
if request.form("DELETE") <> "" Then
sql = "delete from " & table_name
sql = sql & " where 1=1 and " & replace(replace(check_db(request("edit_list")),",", " and "),"@-$","'")

cndb.execute sql
response.Redirect(request.ServerVariables("url"))
end if
if request.Form("EDIT") <> "" then
dont_list_this_field = 1
for i = 0 to rs.fields.count - 1
for lst = 0 to ubound(arr_nolist)
if ucase(arr_nolist(lst)) = ucase(rs.fields(i).name) then dont_list_this_field = 0
next
if dont_list_this_field <> 0 then
if rs.fields(i).type = 3 then
if request(rs.fields(i).name) <> "" then
db_values = db_values & "," & rs.fields(i).name & " = " & check_db(request(rs.fields(i).name))
else
db_values = db_values & "," & rs.fields(i).name & " = NULL"
end if
else
db_values = db_values & "," & rs.fields(i).name & " = '" & check_db(request(rs.fields(i).name)) & "'"
end if
end if
dont_list_this_field = 1
next
sql = "update " & table_name & " set " & mid(db_values,2)
response.Write "Record Edited."
sql = sql & " where 1=1 and " & replace(replace(check_db(request("edit_list")),",", " and "),"@-$","'")
cndb.execute sql
response.Redirect(request.ServerVariables("url"))
end if

call show_records(table_name,no_list)
end sub

sub show_records(table_name,no_list)
dont_list_this_field = 1
set rs = server.CreateObject("adodb.recordset")
rs.open "select * from " & table_name, cndb
arr_nolist = split(no_list,",")
%><BR><BR>
<table bgcolor="#000066" cellpadding="5" cellspacing="1" border="0">
<TR>
<% for i = 0 to rs.fields.count - 1
for lst = 0 to ubound(arr_nolist)
if ucase(arr_nolist(lst)) = ucase(rs.fields(i).name) then dont_list_this_field = 0
next
if dont_list_this_field <> 0 then
%>
<th bgcolor="#FFFFFF"><%=format_ucase(replace(rs.fields(i).name,"_"," "))%></th>
<%
end if
dont_list_this_field = 1
next %>
</tr>
<%

do until rs.eof
For Each fld in rs.Fields
this_val = fld.value
if this_val <> "" then
if not isnull(this_val) then this_val = replace(this_val,"'","\'")
end if
if fld.type = 3 then
if this_val <> "" then edit_list = edit_list & "," & fld.name & "=" & this_val
else
edit_list = edit_list & "," & fld.name & "=" & "@-$" & this_val & "@-$"
end if
for lst = 0 to ubound(arr_nolist)
if ucase(arr_nolist(lst)) = ucase(fld.name) then dont_list_this_field = 0
next
if dont_list_this_field <> 0 then
if this_val <> "" then
if not isnull(this_val) then this_val = replace(this_val,"\'","'")
end if
dis_table = dis_table & "<td bgcolor=white>" & this_val & "</td>"
end if
dont_list_this_field = 1
next
edit_list = mid(edit_list,2)
'window.location.href = window.location.href.replace("#acc","") + "#acc"
response.Write "<tr style=""cursor:pointer"" onclick=""document.location.href=document.location.href.replace(document.location.search,'') + '?edit_list=" & edit_list & "'"">" & dis_table & "</tr>"
'response.Write "<tr onclick=""alert(document.location.search)"">" & dis_table & "</tr>"

dis_table = ""
edit_list =""
rs.movenext : loop
end sub

function format_ucase(tname)
do while instr(tname, " ")
temp_string = left(tname, instr(tname," " ) -1)
' ucase the first letter
format_ucase = format_ucase & ucase(mid(temp_string, 1,1))
' lcase for the rest of the word
format_ucase = format_ucase & lcase(mid(temp_string,2)) & " "
tname = right(tname, len(tname) - instr(tname," " ))
loop
'send out the rest of the word
format_ucase = format_ucase & ucase(mid(tname, 1,1))
format_ucase = format_ucase & mid(tname,2)
end function

function check_db(vr)
if vr <> "" then
check_db = replace(vr,"'","''")
else
check_db = ""
end if
end function

%>


Back to scripts