Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21266

A coding problem a newbie can't figure out

$
0
0
Hi.

Apologies if this is in the incorrect forum or a possible solution included in a previous post. I admit to being a little out of my depth and not knowing what to search for!

I have found a piece of VB code that I was able to adapt for a solution to a problem I had creating a new table for my Access database.

Essentially I am trying to change the following recordset

"Name", "3"
"Name", "0"
"Name", "1"
"Name", "4"
"Name", "2"
"Name", "8"

to

"Name" "824103"

It works 99% of the time but occasionally is returning

"Name", " "

even though the values are in the table it refers to.

Any ideas what could be causing the problem? Or is there a better way to achieve what I am trying to do?

*********************

Here is the code I am using:

Public Function FixTable() As Boolean
On Error Resume Next

Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
Dim strNAME As String, strFINPOS As String, strautonumber As String

Set db = CurrentDb()
Call RecreateTables(db)

sSQL = "SELECT autonumber, NAME, FINPOS, RACENUMBER FROM LastSixNH " _
& "ORDER BY NAME, RACENUMBER ASC"
Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)

If Not rst.BOF And Not rst.EOF Then
rst.MoveFirst
strNAME = rst!NAME
strFINPOS = rst!FINPOS
strautonumber = rst!autonumber

rst.MoveNext
Do Until rst.EOF
If strNAME = rst!NAME Then
strFINPOS = strFINPOS & "" & rst!FINPOS
Else
sSQL = "INSERT INTO LastSixNHCopy (autonumber, NAME, FINPOS) " _
& "VALUES('" & strautonumber & "','" & strNAME & "','" & strFINPOS & "')"
db.Execute sSQL
strautonumber = rst!autonumber
strNAME = rst!NAME
strFINPOS = rst!FINPOS
End If
rst.MoveNext
Loop

' Insert Last Record
sSQL = "INSERT INTO LastSixNHCopy (NAME, FINPOS) " _
& "VALUES('" & strautonumber & "','" & strNAME & "','" & strFINPOS & "')"
db.Execute sSQL
End If

Set rst = Nothing
Set db = Nothing


End Function

Private Function RecreateTables(ByRef dbs As DAO.Database)
On Error Resume Next
Dim sSQL As String



' Delete Table, if exists
If DCount("*", "MsysObjects", "[Name]='LastSixNHCopy'") = 1 Then
DoCmd.DeleteObject acTable, "LastSixNHCopy"
End If

' Create Temp Table
sSQL = "SELECT autonumber, NAME, FINPOS INTO LastSixNHCopy " _
& "FROM LastSixNH WHERE 1 = 0;"
dbs.Execute sSQL

End Function

Viewing all articles
Browse latest Browse all 21266

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>