G
Guest
I have been using the following code from Allen Browne to select a random
percentage of records on the open event of an unbound report.
Private Sub Report_Open(Cancel As Integer)
Dim strSql As String
Dim strPercentage As String
Dim strMsg As String
strPercentage = InputBox("How many percent?")
If IsNumeric(strPercentage) Then
If strPercentage >= 1 And strPercentage <= 100 Then
strSql = "SELECT TOP " & strPercentage & " PERCENT TblAssets.*
FROM TblAssets ORDER BY Rnd([AssetID]);"
Else
strMsg = "Percentage must be between 1 and 100."
End If
Else
strMsg = "No percentage entered."
End If
If Len(strMsg) > 0 Then
MsgBox strMsg, vbExclamation, "Report not opened."
Else
Randomize
Me.RecordSource = strSql
End If
End Sub
The code works well but I am struggling to write functioning code to
retrieve random records that meet a particular criteria. I can use a query
to select the records required and then build a report from that but I like
the idea of using code directly in the report. Please note that I am only
just learning to use SQL. The code I have tried so far is –
Private Sub Report_Open(Cancel As Integer)
Dim strSql As String
Dim strPercentage As String
Dim strMsg As String
Dim strWhere As String
strPercentage = InputBox("How many percent?")
If IsNumeric(strPercentage) Then
If strPercentage >= 1 And strPercentage <= 100 Then
strSql = "SELECT TOP " & strPercentage & " PERCENT TblAssets.*â€
strWhere = "TblAssets.SCAItem = -1"
FROM TblAssets ORDER BY Rnd([AssetID]);
Else
strMsg = "Percentage must be between 1 and 100."
End If
Etc
I keep getting a compile error on the FROM line. I’ve researched all the
discussion groups and the web without finding an answer to my problem. Any
help would be very much appreciated to get me on the right track. Thanks in
anticipation.
Joe
percentage of records on the open event of an unbound report.
Private Sub Report_Open(Cancel As Integer)
Dim strSql As String
Dim strPercentage As String
Dim strMsg As String
strPercentage = InputBox("How many percent?")
If IsNumeric(strPercentage) Then
If strPercentage >= 1 And strPercentage <= 100 Then
strSql = "SELECT TOP " & strPercentage & " PERCENT TblAssets.*
FROM TblAssets ORDER BY Rnd([AssetID]);"
Else
strMsg = "Percentage must be between 1 and 100."
End If
Else
strMsg = "No percentage entered."
End If
If Len(strMsg) > 0 Then
MsgBox strMsg, vbExclamation, "Report not opened."
Else
Randomize
Me.RecordSource = strSql
End If
End Sub
The code works well but I am struggling to write functioning code to
retrieve random records that meet a particular criteria. I can use a query
to select the records required and then build a report from that but I like
the idea of using code directly in the report. Please note that I am only
just learning to use SQL. The code I have tried so far is –
Private Sub Report_Open(Cancel As Integer)
Dim strSql As String
Dim strPercentage As String
Dim strMsg As String
Dim strWhere As String
strPercentage = InputBox("How many percent?")
If IsNumeric(strPercentage) Then
If strPercentage >= 1 And strPercentage <= 100 Then
strSql = "SELECT TOP " & strPercentage & " PERCENT TblAssets.*â€
strWhere = "TblAssets.SCAItem = -1"
FROM TblAssets ORDER BY Rnd([AssetID]);
Else
strMsg = "Percentage must be between 1 and 100."
End If
Etc
I keep getting a compile error on the FROM line. I’ve researched all the
discussion groups and the web without finding an answer to my problem. Any
help would be very much appreciated to get me on the right track. Thanks in
anticipation.
Joe