0

I've a ComboBox but the problem is that when I try to save the value selected, 'cause I load the ComboBox with values of SQLServer and the applications shows me the next error:

[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'celular'.

I load the ComboBox with values of SQL:

Private Sub Form_Load()
   Set rs = New Recordset
   rs.CursorLocation = adUseServer

   Call IniciarConexion

   CargaIDTipoNumero
End Sub

Private Sub CargaIDTipoNumero()
    cmbAddExample(indice).Clear
    rs.Open "SELECT tipo FROM tipo_Numero", cnn, adOpenDynamic, adLockOptimistic
    Do While rs.EOF = False
        cmbAddExample(indice).AddItem rs!tipo
        rs.MoveNext
    Loop
    rs.Close
End Sub

Private Sub IniciarConexion()
    Set cnn = New ADODB.Connection
    With cnn
        .CursorLocation = adUseClient
        .Open "PROVIDER=MSDASQL;driver={SQL Server};server=server;uid=uid;pwd=pwd;database=database;"
    End With
End Sub

The table where saves the values of ComboBox:

CREATE TABLE ejTres(
    combo int
)

The table with I load the ComboBox:

CREATE TABLE tipo_Numero(
    idTipo INT IDENTITY (1,1) NOT NULL, 
    tipo VARCHAR (10) NOT NULL, 
    CONSTRAINT pk_tipo PRIMARY KEY(idTipo)
)

INSERT INTO tipo_Numero(tipo)
VALUES('celular'), ('fijo')

And the Button where I save the values:

Private Sub btnGuardar_Click()
Dim i As Integer
Dim CM As ADODB.Command

Set CM = New ADODB.Command
Set CM.ActiveConnection = cnn
    CM.CommandType = adCmdText
    CM.CommandText = "INSERT INTO ejTres (combo) VALUES (?)"
    CM.Parameters.Append CM.CreateParameter("@combo", adInteger, , , cmbAddExample(i))
    CM.Execute , , adExecuteNoRecords
End Sub
Franqo Balsamo
  • 171
  • 1
  • 15

1 Answers1

1

Focusing only on the issue of how to get the Identity value into your table, try something like the following:

''as you load the combo, also save the identity value

Do While rs.EOF = False
   cmbAddExample(indice).AddItem rs!tipo
   cmbAddExample(indice).ItemData(cmbAddExample(indice).NewIndex) = rs!idTipo
   rs.MoveNext
Loop


''now retrieve the identity value when creating the parameter

id = cmbAddExample(i).ItemData(cmbAddExample(i).ListIndex)

Set CM = New ADODB.Command
Set CM.ActiveConnection = cnn
CM.CommandType = adCmdText
CM.CommandText = "INSERT INTO ejTres (combo) VALUES (?)"
CM.Parameters.Append CM.CreateParameter("@combo", adInteger, , , id)
CM.Execute , , adExecuteNoRecords
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25