1
My question is as follows: I developed a VBA form with 20 fields, which registers in the Bdados spreadsheet, also inserting an ID. What I want is to find and copy a record of the spreadsheet to the form, filling all its fields then change some of them and write to Bdados with new ID.
In Excel it will be the same as copying a row , pasting into a new row and changing the ID and some cells.
It remains to add that the ID is automatically inserted when the form is opened.
I am a layman in VBA Excel and I still can’t find an example to use as inspiration for my project.
Thank you. For the help.
THE CODE ALREADY DEVELOPED IS AS FOLLOWS::
Private Sub BTN_GRAVAR_Click()
Dim NR As Long
Dim DATA_MATRICULA As Date
Dim DATA_INICIAL As Date
Dim DATA_FINAL As Date
Folha2.Select
Range("A3").End(xlDown).Select
NR = ActiveCell.Row
Range("a65536").End(xlUp).Offset(1, 0).Select
ActiveCell.Offset(0, 0).Value = LBL_NR.Caption
ActiveCell.Offset(0, 1).Value = txtident.Text
ActiveCell.Offset(0, 2).Value = txtmatricula.Text
ActiveCell.Offset(0, 3).Value = txtdata.Text
ActiveCell.Offset(0, 4).Value = txtcilindrada.Text
ActiveCell.Offset(0, 5).Value = txtpeso.Text
ActiveCell.Offset(0, 6).Value = Cbocombustivel.Text
ActiveCell.Offset(0, 7).Value = cbolugares.Text
ActiveCell.Offset(0, 8).Value = cbotipo.Text
ActiveCell.Offset(0, 9).Value = cbocategoria.Text
ActiveCell.Offset(0, 10).Value = txtpneuf.Text
ActiveCell.Offset(0, 11).Value = txtpneut.Text
ActiveCell.Offset(0, 12).Value = cboseguradora.Text
ActiveCell.Offset(0, 13).Value = txtapolice.Text
ActiveCell.Offset(0, 14).Value = txtvalorizacao.Text
ActiveCell.Offset(0, 15).Value = txtinicial.Text
ActiveCell.Offset(0, 16).Value = txtfinal.Text
ActiveCell.Offset(0, 17).Value = Txtvalor.Text
ActiveCell.Offset(0, 18).Value = txttaxa.Text
ActiveCell.Offset(0, 19).Value = cbocentro.Text
Columns("A:T").AutoFit
txtident.Text = ""
txtmatricula.Text = ""
txtdata.Text = ""
txtcilindrada.Text = ""
txtpeso.Text = ""
Cbocombustivel.Value = ""
cbolugares.Value = ""
cbotipo.Value = ""
cbocategoria.Value = ""
txtpneuf.Text = ""
txtpneut.Text = ""
cboseguradora.Value = ""
txtapolice.Text = ""
txtvalorizacao.Text = ""
txtinicial.Text = ""
txtfinal.Text = ""
Txtvalor.Text = ""
txttaxa.Text = ""
cbocentro.Value = ""
Me.LBL_NR = Folha2.Range("a65536").End(xlUp).Row - 1
txtident.SetFocus
End Sub
Private Sub BTN_Sair_Click()
Unload Me
End Sub
Private Sub txtdata_Change()
If Len(Me.txtdata.Text) = 2 Then
Me.txtdata.Text = Me.txtdata.Text & "/"
Me.txtdata.SelStart = 4
ElseIf Len(Me.txtdata.Text) = 5 Then
Me.txtdata.Text = Me.txtdata.Text & "/"
Me.txtdata.SelStart = 7
ElseIf Len(Me.txtdata.Text) = 10 Then
Me.txtcilindrada.SetFocus
End If
End Sub
Private Sub txtinicial_Change()
If Len(Me.txtinicial.Text) = 2 Then
Me.txtinicial.Text = Me.txtinicial.Text & "/"
Me.txtinicial.SelStart = 4
ElseIf Len(Me.txtinicial.Text) = 5 Then
Me.txtinicial.Text = Me.txtinicial.Text & "/"
Me.txtinicial.SelStart = 7
ElseIf Len(Me.txtinicial.Text) = 10 Then
Me.txtfinal.SetFocus
End If
End Sub
Private Sub txtfinal_Change()
If Len(Me.txtfinal.Text) = 2 Then
Me.txtfinal.Text = Me.txtfinal.Text & "/"
Me.txtfinal.SelStart = 4
ElseIf Len(Me.txtfinal.Text) = 5 Then
Me.txtfinal.Text = Me.txtfinal.Text & "/"
Me.txtfinal.SelStart = 7
ElseIf Len(Me.txtfinal.Text) = 10 Then
Me.Txtvalor.SetFocus
End If
End Sub
Private Sub txtmatricula_Change()
If Len(Me.txtmatricula.Text) = 2 Then
Me.txtmatricula.Text = Me.txtmatricula.Text & "-"
Me.txtmatricula.SelStart = 4
ElseIf Len(Me.txtmatricula.Text) = 5 Then
Me.txtmatricula.Text = Me.txtmatricula.Text & "-"
Me.txtmatricula.SelStart = 8
ElseIf Len(Me.txtmatricula.Text) = 8 Then
Me.txtdata.SetFocus
End If
End Sub
Private Sub UserForm_Initialize()
Me.LBL_NR = Folha2.Range("a65536").End(xlUp).Row
End Sub
Include in your question the code of what you have already managed to do.
– Leandro Angelo
I suggest you break your question into several smaller ones ( MVCE ). And explain exactly what each variable is, for example if
txtdata
is aTextBox
? And what exactly is the problem you are encountering and that this code did not work.– danieltakeshi
The code works. The form records perfectly in the BD table. What I am not able to do is locate an already recorded record, copy it to the form, and after changing some fields, re-register it as a new record, with new ID nr.
– Júlio Faria