B
Bianca.Sayan
I hope this is the appropriate group for a question like this, which
involves Queries, Recordset, and VB. Okay, its looking as though I
might be over my head with some code here. Any help would be
appreciated.
I have a table in my microsoft access that looks a bit like this:
Invasive Species
______________
bears
bears, raccoons
raccoons
raccoons, skunks
skunks
skunks
raccoons
What I'd like to do with this table is create a new recordset where
the records with more than one animal delimited by a comma becomes 2
records
so one record that is "bears, raccoons" becomes 2 records, "bears" and
"raccoons" respectively.
so, it will look like this.
Invasive Species
______________
bears
bears
raccoons
raccoons
raccoons
skunks
skunks
skunks
raccoons
Then, I want to tally all the distinct entries up so that it will look
like this
Invasive Species Count
________________________________________________________
bears 2
skunks 3
raccoons 4
Then I'd like to throw that into a report.....
I've written some code. I suspect its bad. My first clue is that it
doesnt work. My second clue is I'm not sure I constructively know how
to go about fixing it. Here is the code:
_____________________________________________________________________________
Dim db As Database
Dim rs As Recordset
Dim thequery As QueryDef
Dim mysqlquery As String
Dim newthequery As QueryDef
Dim newmysqlquery As String
Dim newrs As Recordset
Dim finalrs As Recordset
Dim shorttemp As String
Dim longtemp As String
Set mysqlquery = "SELECT [Part F - Biodiversity #11-21].F14v1 AS
F14v1 FROM [Part F - Biodiversity #11-21] WHERE (((IsNull([F14v1])) =
False));"
Set db = CurrentDb()
Set thequery = db.CreateQueryDef("Invasives", mysqlquery)
Set rs = thequery.OpenRecordset()
Print rs.RecordCount
Me.Text28.Value = rs.Fields("F14v1").Value
While (rs.EOF = False)
longtemp = rs.Fields("F14v1").Value
While (Not InStr("", longtemp, ",") = 0)
shorttemp = Left(longtemp, (InStr("", ",", longtemp) -
1)).Trim
longtemp = Right(longtemp, (InStr("", ",", longtemp) +
1)).Trim
newrs.AddNew
newrs.Fields("LongUncountedList").Value = shorttemp
End If
newrs.AddNew
newrs.Fields("LongUncountedList").Value = longtemp
rs.MoveNext
End If
Set newmysqlquery = "SELECT DISTINCT LongUncountedList AS names,
Count(LongUncountedList) AS tally FROM Invasives WHERE
(((IsNull(LongUncountedList)) = False)) GROUP BY LongUncountedList;"
Set newthequery = db.CreateQueryDef("FinalInvasives",
newmysqlquery)
Me.RecordSource = newthequery.OpenRecordset()
_____________________________________________________________________________
So, can anyone give me a hint as to what the problem(s) are? Or
alternative methods of going about it?
I suspect it relates to my defiencies in understanding exactly how to
use VB to interact with the Access objects.
involves Queries, Recordset, and VB. Okay, its looking as though I
might be over my head with some code here. Any help would be
appreciated.
I have a table in my microsoft access that looks a bit like this:
Invasive Species
______________
bears
bears, raccoons
raccoons
raccoons, skunks
skunks
skunks
raccoons
What I'd like to do with this table is create a new recordset where
the records with more than one animal delimited by a comma becomes 2
records
so one record that is "bears, raccoons" becomes 2 records, "bears" and
"raccoons" respectively.
so, it will look like this.
Invasive Species
______________
bears
bears
raccoons
raccoons
raccoons
skunks
skunks
skunks
raccoons
Then, I want to tally all the distinct entries up so that it will look
like this
Invasive Species Count
________________________________________________________
bears 2
skunks 3
raccoons 4
Then I'd like to throw that into a report.....
I've written some code. I suspect its bad. My first clue is that it
doesnt work. My second clue is I'm not sure I constructively know how
to go about fixing it. Here is the code:
_____________________________________________________________________________
Dim db As Database
Dim rs As Recordset
Dim thequery As QueryDef
Dim mysqlquery As String
Dim newthequery As QueryDef
Dim newmysqlquery As String
Dim newrs As Recordset
Dim finalrs As Recordset
Dim shorttemp As String
Dim longtemp As String
Set mysqlquery = "SELECT [Part F - Biodiversity #11-21].F14v1 AS
F14v1 FROM [Part F - Biodiversity #11-21] WHERE (((IsNull([F14v1])) =
False));"
Set db = CurrentDb()
Set thequery = db.CreateQueryDef("Invasives", mysqlquery)
Set rs = thequery.OpenRecordset()
Print rs.RecordCount
Me.Text28.Value = rs.Fields("F14v1").Value
While (rs.EOF = False)
longtemp = rs.Fields("F14v1").Value
While (Not InStr("", longtemp, ",") = 0)
shorttemp = Left(longtemp, (InStr("", ",", longtemp) -
1)).Trim
longtemp = Right(longtemp, (InStr("", ",", longtemp) +
1)).Trim
newrs.AddNew
newrs.Fields("LongUncountedList").Value = shorttemp
End If
newrs.AddNew
newrs.Fields("LongUncountedList").Value = longtemp
rs.MoveNext
End If
Set newmysqlquery = "SELECT DISTINCT LongUncountedList AS names,
Count(LongUncountedList) AS tally FROM Invasives WHERE
(((IsNull(LongUncountedList)) = False)) GROUP BY LongUncountedList;"
Set newthequery = db.CreateQueryDef("FinalInvasives",
newmysqlquery)
Me.RecordSource = newthequery.OpenRecordset()
_____________________________________________________________________________
So, can anyone give me a hint as to what the problem(s) are? Or
alternative methods of going about it?
I suspect it relates to my defiencies in understanding exactly how to
use VB to interact with the Access objects.