I am using Access 2000. I am trying to set up a report with dynamic column headings and text boxes. I have four crosstab queries. I have combined the results of these queries into one query that is the recordsource for the report. I have the following code for making dynamic columns:
Private Sub Report_Open(Cancel As Integer)
Dim intColCount As Integer
Dim i As Integer
Dim strName As String, strSQL As String
Dim Length As Integer, Position As Integer
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
On Error Resume Next
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
strSQL = "Select * from [" & Me.RecordSource & "] "
rst.Open Source:=strSQL, ActiveConnection:=cnn, LockType:=adLockReadOnly
intColCount = rst.Fields.Count
MsgBox intColCount
intControlCount = Me.Detail.Controls.Count
' Fill in information for the necessary controls.
For i = 1 To intColCount
If (i <> 4 Or i <> 8 Or i <> 12) Then
strName = rst.Fields(i).Name
MsgBox strName
Me.Controls("txtData" & i).ControlSource = strName
Length = Len(strName)
Position = InStr(1, strName, ".", vbTextCompare)
strName = Right(strName, (Length - Position))
Me.Controls("lblHeader" & i).Caption = strName
End If
Next i
' Close the recordset.
rst.Close
cnn.Close
End Sub
The code is not reading the recordset properly. intcolcount is returning 0. Therefore the code is not getting past the For Loop. I am not too familiar with ADO. I tried using DAO also but that did not work. Any help would be greatly appreciated.
Thanks,
Jeff Weisman
Private Sub Report_Open(Cancel As Integer)
Dim intColCount As Integer
Dim i As Integer
Dim strName As String, strSQL As String
Dim Length As Integer, Position As Integer
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
On Error Resume Next
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
strSQL = "Select * from [" & Me.RecordSource & "] "
rst.Open Source:=strSQL, ActiveConnection:=cnn, LockType:=adLockReadOnly
intColCount = rst.Fields.Count
MsgBox intColCount
intControlCount = Me.Detail.Controls.Count
' Fill in information for the necessary controls.
For i = 1 To intColCount
If (i <> 4 Or i <> 8 Or i <> 12) Then
strName = rst.Fields(i).Name
MsgBox strName
Me.Controls("txtData" & i).ControlSource = strName
Length = Len(strName)
Position = InStr(1, strName, ".", vbTextCompare)
strName = Right(strName, (Length - Position))
Me.Controls("lblHeader" & i).Caption = strName
End If
Next i
' Close the recordset.
rst.Close
cnn.Close
End Sub
The code is not reading the recordset properly. intcolcount is returning 0. Therefore the code is not getting past the For Loop. I am not too familiar with ADO. I tried using DAO also but that did not work. Any help would be greatly appreciated.
Thanks,
Jeff Weisman