Jump to content

Recommended Posts

Posted

Boa tarde!

Bem precisava de fazer o seguinte.

Preciso de uma função para gerar um código de barras aleatório diferente daqueles que já existem numa base de dados.

o meu problema é que na base de dados em questão existem códigos com letras e assim obtenho um erro na consulta.

Alguma ideia?

Posted
Shared Function GetRandomCode(ByVal length As Int16) As String
       Dim num_characters As Integer
       Dim i As Integer
       Dim txt As String = String.Empty
       Dim ch As Integer
       num_characters = 20
       Randomize()
       For i = 1 To num_characters
           ch = Int((26 + 26 + 10) * Rnd())
           If ch < 26 Then
               txt = txt & Chr(ch + Asc("A"))
           ElseIf ch < 2 * 26 Then
               ch = ch - 26
               txt = txt & Chr(ch + Asc("a"))
           Else
               ch = ch - 26 - 26
               txt = txt & Chr(ch + Asc("0"))
           End If
       Next i

       Return txt

   End Function

Com esta funçao já funciona, depois e so fazer uma consulta na bd, caso exista mando gerar outro, caso não exista tá novo... 🙂

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.