Hola buenas,
Estoy haciendo un código que si os resumo el funcionamiento es este:
leo un txt, y voy rellenando un excel utilizando alguna base de datos access ( OLEDB)
y la informacion del txt.
Pero como puedo hacer para que al leer un valor alfanumerico de un acces este se incremente en uno y se ponga en el excel?
me explico : leo el valor del access: cya_0000, cya_0001 (pero solo utilizo el segundo valor)
y dependiendo de las lineas del txt se ponen uno o más. 1 por linea.
EJEMPLO:
y al escribirlo en el excel :( txt con una línea) cya_0002
(txt con 2 lineas) cya_0002 y cya_0003
al terminar esto: deberia de actualizar el access.
ACCESS FINAL:
cya_00000, cya_00001
cya_00002, cya_00004 (dando por hecho que el txt tiene 3 lineas)
No se si me he explicado bien... en caso de que no entendais preguntar y os contestare.
base de datos="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Usuario\Desktop\prueba\TABLAS_PRUEBA.accdb"
Cita de: PETTETE en 20 Septiembre 2017, 17:00 PMcomo puedo hacer para que al leer un valor alfanumerico de un acces este se incremente en uno y se ponga en el excel?
Teniendo un patrón tal que: "Mi_Prefijo_0000"
1. Separar el String en dos partes o tokens, la alfabética (el prefijo), y la numérica (el índice). "Mi_Prefijo_" + "0000"
2. Incrementar el valor de la parte numérica preservando la longitud del caracter de relleno, en este caso el Cero. ( ej. 0000 -> 0001 -> 0002 -> etc... )
3. Concatenar de nuevo la parte alfabética con la numérica, dando como resultado: "Mi_Prefijo_0001"
Fin.
No es algo complicado. Aquí tienes una función de uso genérico que he desarrollado la cual puedes adaptar a tus necesidades:
Public Shared Function IncrementIndexPattern(ByVal pattern As String, ByVal position As Integer, ByVal fillChar As Char) As String
Dim curSuffix As String = pattern.Substring(position)
Dim newSuffix As String
Dim suffixLen As Integer = curSuffix.Length
Dim index As Integer
If Not Integer.TryParse(If(fillChar = "0"c, curSuffix, curSuffix.TrimStart(fillChar)), index) Then
Throw New FormatException("The value does not have a valid numeric format.")
End If
newSuffix = Convert.ToString(index + 1).PadLeft(suffixLen, fillChar)
If (newSuffix.Length > suffixLen) Then
' -= Deposite su control de errores aquí =-
' La longitud del nuevo índice es mayor que la capacidad de ceros.
' Ej. "pattern_12345" es más largo que "pattern_000".
Throw New IndexOutOfRangeException()
Else
Return pattern.Substring(0, position) & newSuffix
End If
End Function
Ejemplo de uso:
' Programatically building a pattern.
Dim prefix As String = "My_Pattern_"
Dim fillChar As Char = "#"c
Dim fillCount As Integer = 4
Dim firstIdx As String = New String(fillChar, (fillCount - 1)) & "0" ' ###0
Dim pattern As String = (prefix & firstIdx) ' My_Pattern_###0
Debug.WriteLine(pattern)
' Setting the loop values.
Dim idxStartPos As Integer = prefix.Length ' Or also: (pattern.LastIndexOf("_"c) + 1)
Dim maxIndexCount As Integer = Convert.ToInt32(New String("9"c, fillCount)) ' Max possible index to fill.
For i As Integer = 0 To (maxIndexCount - 1)
pattern = IncrementIndexPattern(pattern, idxStartPos, fillChar)
Debug.WriteLine(pattern)
Next i
Resultado de ejecución:
My_Pattern_###0
My_Pattern_###1
My_Pattern_###2
My_Pattern_###3
My_Pattern_###4
My_Pattern_###5
My_Pattern_###6
My_Pattern_###7
My_Pattern_###8
My_Pattern_###9
My_Pattern_##10
...
My_Pattern_9990
My_Pattern_9991
My_Pattern_9992
My_Pattern_9993
My_Pattern_9994
My_Pattern_9995
My_Pattern_9996
My_Pattern_9997
My_Pattern_9998
My_Pattern_9999
Saludos.