PC Review


Reply
Thread Tools Rate Thread

Creating a string from a query in vba

 
 
StuJol
Guest
Posts: n/a
 
      5th May 2010
using access 2003 i have a qry that returns top 5 values

1 Item1 123
2 Item2 101
3 Item3 32
4 Item4 2
5 Item5 76

im trying to create a function that retuens a string like
"Item1 123, Item2 101, Item3 32" etc etc

I can create a function that returns the first row but not all 5 rows. can
anyone help please??
 
Reply With Quote
 
 
 
 
Daniel Pineault
Guest
Posts: n/a
 
      5th May 2010
You could use a function such as:

Function GenStr()
On Error GoTo Error_Handler
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("YourQueryName")

If rs.RecordCount <> 0 Then
With rs
.MoveFirst
Do While Not .EOF
GenStr = GenStr & ![QueryFieldName1] & " " &
![QueryFieldName2] & ", "
.MoveNext
Loop
End With

'Remove trailing , if applicable
If Right(GenStr, 2) = ", " Then
GenStr = Left(GenStr, Len(GenStr) - 2)
End If
Else
'No records returned by your table or query
GenStr = ""
End If

Error_Handler_Exit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: GenStr" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.



"StuJol" wrote:

> using access 2003 i have a qry that returns top 5 values
>
> 1 Item1 123
> 2 Item2 101
> 3 Item3 32
> 4 Item4 2
> 5 Item5 76
>
> im trying to create a function that retuens a string like
> "Item1 123, Item2 101, Item3 32" etc etc
>
> I can create a function that returns the first row but not all 5 rows. can
> anyone help please??

 
Reply With Quote
 
StuJol
Guest
Posts: n/a
 
      6th May 2010
Daniel, Just looking at your response and are getting error messages, my code
is as below and im getting error 3061, too few parameters when the code
executes line Set rs = db.OpenRecordset("Alarms - Module Totals")

any ideas what im doing wrong??


Function GenStr() As String
On Error GoTo Error_Handler

Dim db As Database
Dim rs As Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("Alarms - Module Totals")
MsgBox "1"
If rs.RecordCount <> 0 Then
With rs
.MoveFirst
Do While Not .EOF
GenStr = GenStr & ![Module] & " " & ![CountOfModule] & ", "
.MoveNext
Loop
End With

'Remove trailing , if applicable
If Right(GenStr, 2) = ", " Then
GenStr = Left(GenStr, Len(GenStr) - 2)
End If
Else
'No records returned by your table or query
GenStr = ""
End If

Error_Handler_Exit:
On Error Resume Next
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & Err.Number & vbCrLf & "Error Source: GenStr" & vbCrLf &
"Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function



"Daniel Pineault" wrote:

> You could use a function such as:
>
> Function GenStr()
> On Error GoTo Error_Handler
> Dim db As Database
> Dim rs As Recordset
>
> Set db = CurrentDb()
> Set rs = db.OpenRecordset("YourQueryName")
>
> If rs.RecordCount <> 0 Then
> With rs
> .MoveFirst
> Do While Not .EOF
> GenStr = GenStr & ![QueryFieldName1] & " " &
> ![QueryFieldName2] & ", "
> .MoveNext
> Loop
> End With
>
> 'Remove trailing , if applicable
> If Right(GenStr, 2) = ", " Then
> GenStr = Left(GenStr, Len(GenStr) - 2)
> End If
> Else
> 'No records returned by your table or query
> GenStr = ""
> End If
>
> Error_Handler_Exit:
> On Error Resume Next
> rs.Close
> Set rs = Nothing
> Set db = Nothing
> Exit Function
>
> Error_Handler:
> MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
> "Error Number: " & _
> Err.Number & vbCrLf & "Error Source: GenStr" & vbCrLf & "Error
> Description: " & _
> Err.Description, vbCritical, "An Error has Occured!"
> Resume Error_Handler_Exit
> End Function
> --
> Hope this helps,
>
> Daniel Pineault
> http://www.cardaconsultants.com/
> For Access Tips and Examples: http://www.devhut.net
> Please rate this post using the vote buttons if it was helpful.
>
>
>
> "StuJol" wrote:
>
> > using access 2003 i have a qry that returns top 5 values
> >
> > 1 Item1 123
> > 2 Item2 101
> > 3 Item3 32
> > 4 Item4 2
> > 5 Item5 76
> >
> > im trying to create a function that retuens a string like
> > "Item1 123, Item2 101, Item3 32" etc etc
> >
> > I can create a function that returns the first row but not all 5 rows. can
> > anyone help please??

 
Reply With Quote
 
StuJol
Guest
Posts: n/a
 
      6th May 2010
it appears to be giving errors as a result of opening a qry that is based on
another qry with criteria. is there any way round this??

"StuJol" wrote:

> Daniel, Just looking at your response and are getting error messages, my code
> is as below and im getting error 3061, too few parameters when the code
> executes line Set rs = db.OpenRecordset("Alarms - Module Totals")
>
> any ideas what im doing wrong??
>
>
> Function GenStr() As String
> On Error GoTo Error_Handler
>
> Dim db As Database
> Dim rs As Recordset
>
> Set db = CurrentDb()
> Set rs = db.OpenRecordset("Alarms - Module Totals")
> MsgBox "1"
> If rs.RecordCount <> 0 Then
> With rs
> .MoveFirst
> Do While Not .EOF
> GenStr = GenStr & ![Module] & " " & ![CountOfModule] & ", "
> .MoveNext
> Loop
> End With
>
> 'Remove trailing , if applicable
> If Right(GenStr, 2) = ", " Then
> GenStr = Left(GenStr, Len(GenStr) - 2)
> End If
> Else
> 'No records returned by your table or query
> GenStr = ""
> End If
>
> Error_Handler_Exit:
> On Error Resume Next
> rs.Close
> Set rs = Nothing
> Set db = Nothing
> Exit Function
>
> Error_Handler:
> MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
> "Error Number: " & Err.Number & vbCrLf & "Error Source: GenStr" & vbCrLf &
> "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
> Resume Error_Handler_Exit
> End Function
>
>
>
> "Daniel Pineault" wrote:
>
> > You could use a function such as:
> >
> > Function GenStr()
> > On Error GoTo Error_Handler
> > Dim db As Database
> > Dim rs As Recordset
> >
> > Set db = CurrentDb()
> > Set rs = db.OpenRecordset("YourQueryName")
> >
> > If rs.RecordCount <> 0 Then
> > With rs
> > .MoveFirst
> > Do While Not .EOF
> > GenStr = GenStr & ![QueryFieldName1] & " " &
> > ![QueryFieldName2] & ", "
> > .MoveNext
> > Loop
> > End With
> >
> > 'Remove trailing , if applicable
> > If Right(GenStr, 2) = ", " Then
> > GenStr = Left(GenStr, Len(GenStr) - 2)
> > End If
> > Else
> > 'No records returned by your table or query
> > GenStr = ""
> > End If
> >
> > Error_Handler_Exit:
> > On Error Resume Next
> > rs.Close
> > Set rs = Nothing
> > Set db = Nothing
> > Exit Function
> >
> > Error_Handler:
> > MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
> > "Error Number: " & _
> > Err.Number & vbCrLf & "Error Source: GenStr" & vbCrLf & "Error
> > Description: " & _
> > Err.Description, vbCritical, "An Error has Occured!"
> > Resume Error_Handler_Exit
> > End Function
> > --
> > Hope this helps,
> >
> > Daniel Pineault
> > http://www.cardaconsultants.com/
> > For Access Tips and Examples: http://www.devhut.net
> > Please rate this post using the vote buttons if it was helpful.
> >
> >
> >
> > "StuJol" wrote:
> >
> > > using access 2003 i have a qry that returns top 5 values
> > >
> > > 1 Item1 123
> > > 2 Item2 101
> > > 3 Item3 32
> > > 4 Item4 2
> > > 5 Item5 76
> > >
> > > im trying to create a function that retuens a string like
> > > "Item1 123, Item2 101, Item3 32" etc etc
> > >
> > > I can create a function that returns the first row but not all 5 rows. can
> > > anyone help please??

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a string based on a query bvdahl Microsoft Access Form Coding 1 14th Apr 2010 04:41 PM
Date string problem in query string when posting back =?Utf-8?B?SnVsaWEgQg==?= Microsoft ASP .NET 5 29th Aug 2007 03:08 PM
creating a string with multiple entries for query parameter =?Utf-8?B?SlA=?= Microsoft Access 1 19th Apr 2006 03:10 AM
Creating query from string - NOT clause =?Utf-8?B?TWFCZWxs?= Microsoft Access Queries 7 2nd Dec 2004 05:34 PM
Creating query from SQL string stevecrowhurst@hotmail.com Microsoft Access VBA Modules 1 28th Oct 2003 11:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 PM.