# Algoritmo para completar números en serie

I have a Recordset obtained by the following query:

``````SELECT DISTINCT [Number] FROM NUMBERS WHERE CODE = 7 ORDER BY [Number]
``````

The Recordset will therefore be a list of ordered numbers, eg. [6,14,37,59,81,145]

I would like to generate a long string of numbers made up of only 0s and 1s, where it is all 0s except in positions designated by the numbers in the recordset: eg. 6,14,37,59, etc.

The result would therefore look something like:

``````000000000000000000100000000000000000000001000000000000000000001000000 etc
``````

Asumiendo que `rs` is the Recordset, I have the following code so far. Would this work?

``````intLower = 1
While Not (rs.BOF Or rs.EOF)
intUpper = rs!Number
For intSlot = intLower To intUpper
strOutput = strOutput & IIf(rs!Number = intSlot, 1, 0)
Next intSlot
rs.moveNext
intLower = intUpper + 1
Wend
``````

Note: I realise this is similar to an earlier question of mine, but I am now asking how to do this when the numbers are contained in a Recordset. Also note that I do not want to use a function that converts a recordset to an array, because I am using DAO and apparently the GetRows is problematic.

preguntado el 02 de febrero de 12 a las 10:02

## 1 Respuestas

Qué tal si;

``````dim value as long
dim result as String

do while not rs.EOF
value = rs!Number
If (value > Len(result)) then result = result & String\$(value - Len(result), "0")
Mid\$(result, value, 1) = "1"
rs.moveNext
loop
msgbox result
``````

Edit as they are sorted;

``````dim result as String

do while not rs.EOF
result = result & String\$(rs!Number - Len(result) - 1, "0") & "1"
rs.moveNext
loop
msgbox result
``````

Respondido 02 Feb 12, 15:02

Are you understanding the problem properly? The result would be something like: 00000000000000001000000000000000000000000010000000000000000000000001 etc. - CJ7

For an rs of `3, 4, 5, 8, 10` the output of the above would be the string `"0011100101"` isnt that what your after? - Alex K.

Not trying to be pedantic, but why do have the If statement - are you not relying on rs being sorted? - CJ7

I didn't register the fact they were ordered - Alex K.

OK, pop in a `rs.MoveNext` and I'll award the answer. - CJ7

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.