View Single Post
  #87 (permalink)  
Old 05-30-09, 01:21
pkstormy pkstormy is offline
Moderator
 
Join Date: Dec 2004
Location: Madison, WI
Posts: 3,925
Code to loop through unbound fields on a form

* Loop through and populate unbound fields on a form from a table...

Public Sub FillUnboundForm()
On error resume next
Dim db As Database
Dim rst As Recordset
Dim rfld As DAO.Field
On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("select * from mytable", dbOpenDynaset, dbSeeChanges)

With rst
If .EOF Then
Else
For Each rfld In rst.Fields
Me(rfld.Name) = rfld
Next
End If
End With

End Sub

* Loop through and save unbound fields on a form to a table...

Public Sub saveUnboundForm()
On error resume next
Dim db As Database
Dim rst As Recordset
Dim rfld As DAO.Field, myfield
On Error Resume Next
Set db = CurrentDb
Set rst = db.OpenRecordset("select * from mytable",dbOpenDynaset, dbSeeChanges)

With rst
If .EOF Then
Else
For Each rfld In rst.Fields
rst.Edit
rfld = Me(rfld.Name)
rst(Me(rfld.Name)) = Me(rfld.Name).Value
rst.update
Next
End If
End With

End Sub

* Loop through controls on a form and construct an SQL statement...

private sub butgo_click()
dim ctl as control
dim strSQL as string
for each ctl in me.controls
if typeof ctl is checkbox then
if ctl.value = true then
strSQL = strSQL & ctl.name & ", "
end if
end if
next
strsql = "SELECT " & left$(strSQL, len(strSQL) - 2) & " FROM mytable"
end sub
__________________
Expert Database Programming
MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)
Reply With Quote