Record add from combobox

H

H. Martins

Note: KMaster fields are Key Fields, KSlave Fields are the connections
to the tables where KMasters are located.

Hi have a couple combo boxes connected to two tables. In both first
column has keymaster, second column some data to be selected. Both
comboboxes have RowSource but no ControlSource properties.

Then I have a button to add a record. That button's OnClick is (see
the remarks under after ' ):

Dim rs As DAO.Recordset
Dim strSelect As String

strSelect = "SELECT tblAccoes.lintNSerieAlunoAccaoKMaster,
tblAccoes.lintNAlunoKSlave, tblAccoes.lintTurmaKSlave FROM tblAccoes;"

Set rs = DBEngine(0)(0).OpenRecordset(strSelect)

'Now, I want to add a record to tblAccoes whose KSlave fields point to
Kmaster fields pointed by cmbbxAccao and cmbbxAluno.

rs.AddNew

[tblAccoes!lintTurmaKSlave] = cmbbxAccao.Column(0) 'this column
has the respective KMaster value
[tblAccoes!lintNAlunoKSlave] = cmbbxAluno.Column(0) 'this column
has the respective KMaster value

rs.Close
Set rs = Nothing

Problem is: no record is added.

Can I have some help, please?

Henry
 
D

Dirk Goldgar

(re-posting, as my original reply hasn't appeared)

H. Martins said:
Note: KMaster fields are Key Fields, KSlave Fields are the connections
to the tables where KMasters are located.

Hi have a couple combo boxes connected to two tables. In both first
column has keymaster, second column some data to be selected. Both
comboboxes have RowSource but no ControlSource properties.

Then I have a button to add a record. That button's OnClick is (see
the remarks under after ' ):

Dim rs As DAO.Recordset
Dim strSelect As String

strSelect = "SELECT tblAccoes.lintNSerieAlunoAccaoKMaster,
tblAccoes.lintNAlunoKSlave, tblAccoes.lintTurmaKSlave FROM tblAccoes;"

Set rs = DBEngine(0)(0).OpenRecordset(strSelect)

'Now, I want to add a record to tblAccoes whose KSlave fields point to
Kmaster fields pointed by cmbbxAccao and cmbbxAluno.

rs.AddNew

[tblAccoes!lintTurmaKSlave] = cmbbxAccao.Column(0) 'this column
has the respective KMaster value
[tblAccoes!lintNAlunoKSlave] = cmbbxAluno.Column(0) 'this column
has the respective KMaster value

rs.Close
Set rs = Nothing

Problem is: no record is added.

Can I have some help, please?


You need

rs.Update

to save the record, before rs.Close.
 
H

H. Martins

Still some problem. Note remarks ###

rs.AddNew

### The following line works ok printing (appending) pointers in
txtbxMemo
txtbxMemo.Value = txtbxMemo.Value & " // " & cmbbxAccao.Column(0)
& " - " & cmbbxAluno.Column(0)

[tblAccoes!lintTurmaKSlave] = cmbbxAccao.Column(0)
[tblAccoes!lintNAlunoKSlave] = cmbbxAluno.Column(0)

### The following line is not executed, seems Access skips in the
above lines
txtbxMemo.Value = txtbxMemo.Value & " ## " & cmbbxAccao.Column(0)
& " - " & cmbbxAluno.Column(0)

rs.Update
rs.Close
Set rs = Nothing

Thanks
Henry
 
D

Dirk Goldgar

H. Martins said:
Still some problem. Note remarks ###

rs.AddNew

### The following line works ok printing (appending) pointers in
txtbxMemo
txtbxMemo.Value = txtbxMemo.Value & " // " & cmbbxAccao.Column(0)
& " - " & cmbbxAluno.Column(0)

[tblAccoes!lintTurmaKSlave] = cmbbxAccao.Column(0)
[tblAccoes!lintNAlunoKSlave] = cmbbxAluno.Column(0)

### The following line is not executed, seems Access skips in the
above lines
txtbxMemo.Value = txtbxMemo.Value & " ## " & cmbbxAccao.Column(0)
& " - " & cmbbxAluno.Column(0)

rs.Update
rs.Close
Set rs = Nothing


Sorry, I didn't read very closely before, and didn't notice the other errors
in your code. These lines are in error:
[tblAccoes!lintTurmaKSlave] = cmbbxAccao.Column(0)
[tblAccoes!lintNAlunoKSlave] = cmbbxAluno.Column(0)

Assuming you are intending to update the fields in the recordset, you should
write:

rs!lintTurmaKSlave = cmbbxAccao.Column(0)
rs!lintNAlunoKSlave = cmbbxAluno.Column(0)

I don't see anything wrong with this line:
txtbxMemo.Value = txtbxMemo.Value & " ## " & cmbbxAccao.Column(0)
& " - " & cmbbxAluno.Column(0)

.... but assuming an error is raised on the preceding lines, you would not
normally come to execute it. Do you have error-handling in place?
 
H

H. Martins

Dirk,

rs!lintTurmaKSlave = cmbbxAccao.Column(0)
rs!lintNAlunoKSlave = cmbbxAluno.Column(0)

It works OK.

When should we use [] and when not?
Do you have error-handling in place?

Oops. I guess it is quite important. Please tell me something about.

Thanks
Henry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top