Итак, это мой интерфейс:
Это мой UserControl:
TextBox = Text1 / ComboBox = cmbAddExample
Это код из UserControl
:
Option Explicit
Dim cnn As Connection
Dim rs As Recordset
Dim sql As Command
Private Sub UserControl_Initialize()
Set rs = New Recordset
rs.CursorLocation = adUseServer
Call IniciarConexion
CargaIDTipoNumero
End Sub
Public Property Get AddType() As String
AddType = cmbAddExample.Text
End Property
Public Property Let AddType(ByVal Value As String)
cmbAddExample.Text = Value
End Property
Public Property Get AddNumber() As String
AddNumber = Text1.Text
End Property
Public Property Let AddNumber(ByVal Value As String)
Text1.Text = Value
End Property
Private Sub CargaIDTipoNumero()
cmbAddExample.Clear
rs.Open "SELECT idTipo, tipo FROM tipo_Numero", cnn, adOpenDynamic, adLockOptimistic
Do While rs.EOF = False
cmbAddExample.AddItem rs!tipo
cmbAddExample.ItemData(cmbAddExample.NewIndex) = rs!idTipo
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
В моей форме у меня есть эти методы для подключения и загрузки данные из SQLServer:
Private Sub Form_Load()
Set rs = New Recordset
rs.CursorLocation = adUseServer
Picture1.Visible = False
Call IniciarConexion
CargaIDTipoNumero
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
Private Sub CargaIDTipoNumero()
cmbAddExample(indice).Clear
rs.Open "SELECT idTipo, tipo FROM tipo_Numero", cnn, adOpenDynamic, adLockOptimistic
Do While rs.EOF = False
cmbAddExample(indice).AddItem rs!tipo
cmbAddExample(indice).ItemData(cmbAddExample(indice).NewIndex) = rs!idTipo
rs.MoveNext
Loop
rs.Close
End Sub
Итак, проблема в следующем:
compilation error sub or function not defined
в строке: cmbAddExample(indice).Clear
Наконец, у меня есть «Guardar» button
, который сохраняет данные в SQLServer:
Private Sub btnGuardar_Click()
Dim i As Integer
Dim id As Integer
Dim CM As ADODB.Command
For i = 0 To indice
id = cmbAddExample(i).ItemData(cmbAddExample(i).ListIndex)
Set CM = New ADODB.Command
Set CM.ActiveConnection = cnn
CM.CommandType = adCmdText
CM.CommandText = "INSERT INTO ejemplo (combo,nombre) VALUES (?,?)"
CM.Parameters.Append CM.CreateParameter("@txt", adInteger, , , id)
CM.Parameters.Append CM.CreateParameter("@comboDos", adInteger, , , Text1(i).Text)
CM.Execute , , adExecuteNoRecords
Next
End Sub
Как загрузить ComboBox с данными SQLServer? И как мне их сохранить?