Insert records into table separated by "," caracter

Luthius

Active Member
Joined
Apr 5, 2011
Messages
324
I have a routine that generates a sequence of numbers like this

"25,24,18,16,12,11,09,08,07,06,05,04,03,02,01"


I would like to insert these numbers separated in my table ordered from lower numbers to higher numbers.


Table Name:tblResults
Fields Name:
number1 (LOWEST NUMBER OF THE SEQUENCE)
number2
...
number15 (HIGHEST NUMBER OF THE SEQUENCE)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Thanks for the links
Well, the problem now is how to use the recordset to insert the values in my table.

Is it possíble give a special hint on how to do it?

Code:
Adapted from http://support.microsoft.com/kb/133135'Note: a zero-based array is assumed (jazz this up by testing for zero or one base arrays and proceeding accordingly)
Public Function BubbleSort(ByVal tempArray As Variant) As Variant
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer


    ' Loop until no more "exchanges" are made.
    Do
        NoExchanges = True
        
        ' Loop through each element in the array.
        For i = 0 To UBound(tempArray) - 1
        
            ' Substitution when element is greater than the element following int
            If tempArray(i) > tempArray(i + 1) Then
                NoExchanges = False
                Temp = tempArray(i)
                tempArray(i) = tempArray(i + 1)
                tempArray(i + 1) = Temp
            End If
        
        Next i
    
    Loop While Not (NoExchanges)
    
    BubbleSort = tempArray


End Function
 
Upvote 0
The request seems little odd, a little counter intuitive to good database design. You usually don't store "calculations" on a table level. It removes the dynamic nature of a database, and can undermine data integrity. For example, what if you stored the "rankings", then changed a value or added values later. Then your rankings may no longer be correct.

Check out this thread form Allen Browne on how to do these type of rankings dynamically: Microsoft Access tips:

If you absolutely have to/want to store them on a table level, one way would be to import them into a temporary tables, sort them in a query, and then use VBA code to loop through the recordset, writing out each record in order.
 
Upvote 0
I'm developing application with numbers as data because I need to store the result of partitions..

Partition Function P -- from Wolfram MathWorld

I am almost there. I would like your assistance on help me to finish it

Code:
Sub insertRecorsInMyTable()Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSequence As String
Dim varString As Variant
Dim varTemp As Variant
Dim maxLenght As Integer


strSequence = "25,24,18,16,12,11,09,08,07,06,05,04,03,02,01"
varString = Split(strSequence, ",")
maxLenght = UBound(varString)
ReDim varTemp(0 To maxLenght)


For i = 0 To maxLenght
    varTemp(i) = CLng(varString(i))
Next i


varTemp = BubbleSort(varTemp)
maxLenght = UBound(varTemp)


Set db = CurrentDb
Set rs = db.OpenRecordset("tblResults", dbOpenTable, dbAppendOnly)


For j = 1 To maxLenght
'rs.AddNew
'rs![number1] = varTemp(j)
Next
'rs.Update
rs.Close
db.Close
End Sub
 
Upvote 0
This entered your array into the lone Numbers field of tblResults:

Code:
Sub BubbleSort()
Dim tempArray As Variant
Dim Temp As Variant
Dim i As Integer
Dim NoExchanges As Integer
tempArray = Array(25, 24, 18, 16, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1)


    ' Loop until no more "exchanges" are made.
    Do
        NoExchanges = True
        
        ' Loop through each element in the array.
        For i = 0 To UBound(tempArray) - 1
        
            ' Substitution when element is greater than the element following int
            If tempArray(i) > tempArray(i + 1) Then
                NoExchanges = False
                Temp = tempArray(i)
                tempArray(i) = tempArray(i + 1)
                tempArray(i + 1) = Temp
            End If
        
        Next i
    
    Loop While Not (NoExchanges)
    

Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset("tblResults", dbOpenDynaset)
For i = LBound(tempArray) To UBound(tempArray)
rs.AddNew
rs!Numbers = tempArray(i)
rs.Update
Next i
End Sub
 
Upvote 0
Thanks for your feedback, but the characteristic of my table is different. I mean, as explained before I have 15 fields to receive the 15 numbers of the sequence. Unfortunately I cannot change it.

Fields Name:
number1 (LOWEST NUMBER OF THE SEQUENCE)
number2
...
number15 (HIGHEST NUMBER OF THE SEQUENCE)
 
Upvote 0

Forum statistics

Threads
1,221,854
Messages
6,162,448
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top