Erro campo valor vazio

carloseg

27-06-2016 22:07:14

Boa noite amigos preciso de uma ajuda, estou desenvolvendo um pequeno cadastro de produtos, tenho 3 textbox que recebem valores. Em cada textbox coloquei o seguinte código.

Código: Selecionar todos

Private Sub txt_cartuchovalorcompativel_AfterUpdate()
txt_cartuchovalorcompativel.Text = Format(txt_cartuchovalorcompativel.Text, "currency")
End Sub

Private Sub txt_cartuchovalorrecarga_afterupdate()
txt_cartuchovalorrecarga.Text = Format(txt_cartuchovalorrecarga.Text, "currency")
End Sub

Private Sub txt_cartuchovalorrevenda_AfterUpdate()
txt_cartuchovalorrevenda.Text = Format(txt_cartuchovalorrevenda.Text, "currency")
End Sub
Para gravar o criei as seguintes variaveis.

Código: Selecionar todos

Private Sub btngravar_cartucho_Click()
Dim valorcartuchorecarga As Currency
Dim valorcartuchocompativel As Currency
Dim valorcartuchorevenda As Currency

valorcartuchorecarga = txt_cartuchovalorrecarga.Text
valorcartuchocompativel = txt_cartuchovalorcompativel.Text
valorcartuchorevenda = txt_cartuchovalorrevenda.Text
Para carregar esses campos para planilha usei o seguinte código.

Código: Selecionar todos

ActiveCell.Offset(0, 10).Value = valorcartuchorecarga
ActiveCell.Offset(0, 11).Value = valorcartuchocompativel
ActiveCell.Offset(0, 12).Value = valorcartuchorevenda
Porem se deixar de preencher um dos textbox com valor, retorna o erro campo vazio. Tentei usar if porem não consigo resolver esse problema, sei que poderia usar um if obrigando a colocação do valor, porem gostaria que o campo se não preenchido recebe-se o valor 0 na planilha se puder ajudar.

Reinaldo

27-06-2016 22:18:38

Faça a validação no momento que for atribuir o valor à variavel

Código: Selecionar todos

If txt_cartuchorrecarga.Text = "" Then
    valorcartuchorecarga = 0
Else
    valorcartuchorecarga = txt_cartuchovalorrecarga.Text
End If

carloseg

27-06-2016 22:43:39

Fiz dessa forma, um teste no primeiro textbox porem da erro na linha - If txt_cartuchorrecarga.Text = "" Then

segue o código botão gravar.

Código: Selecionar todos

' Botão Gravar

Private Sub btngravar_cartucho_Click()
Dim valorcartuchorecarga As Currency
Dim valorcartuchocompativel As Currency
Dim valorcartuchorevenda As Currency

If txt_cartuchorrecarga.Text = "" Then
    valorcartuchorecarga = 0
Else
    valorcartuchorecarga = txt_cartuchovalorrecarga.Text
End If
valorcartuchocompativel = txt_cartuchovalorcompativel.Text
valorcartuchorevenda = txt_cartuchovalorrevenda.Text


Imagem_cartucho_fixa.Visible = True

'Selecionar a célula A1
Range("A1").Select

'Procurar a primeira célula vazia
Do
  If Not (IsEmpty(ActiveCell)) Then
      ActiveCell.Offset(1, 0).Select
  End If
Loop Until IsEmpty(ActiveCell) = True

' Campo nome não pode ser vazio

If txt_cartucho.Text = "" Then
MsgBox "O Campo Modelo não pode ser vazio!", vbOKOnly, "Atenção!"

Else

'Carregar os dados digitados nas caixas de texto para a planilha

ActiveCell.Offset(0, 0).Value = txt_cartuchocodigo.Value
ActiveCell.Offset(0, 1).Value = txt_cartuchodatacadastro.Value
ActiveCell.Offset(0, 2).Value = txt_cartucho.Value
ActiveCell.Offset(0, 3).Value = txt_cartuchomodelo.Value
ActiveCell.Offset(0, 4).Value = combobox_cartuchofabricante.Value
ActiveCell.Offset(0, 5).Value = combobox_cartuchotipo.Value
ActiveCell.Offset(0, 6).Value = combobox_cartuchocor.Value
ActiveCell.Offset(0, 7).Value = txt_cartuchocopias.Value
ActiveCell.Offset(0, 8).Value = combobox_cartuchounidade.Value
ActiveCell.Offset(0, 9).Value = txt_cartuchoimpressoras.Value
ActiveCell.Offset(0, 10).Value = valorcartuchorecarga
ActiveCell.Offset(0, 11).Value = valorcartuchocompativel
ActiveCell.Offset(0, 12).Value = valorcartuchorevenda
ActiveCell.Offset(0, 13).Value = txt_enderecoimagem.Value
txt_cartucho.SetFocus ' foco sobre o campo



'MENSAGEM CARTUCHO CADASTRADO COM SUCESSO

Dim gravadocartucho As String

gravadocartucho = "Cartucho cadastrado com sucesso!"
label_cartuchomensagemgravado = gravadocartucho
label_cartuchomensagemgravado.Visible = True

'Limpar Campos

txt_cartuchocodigo.Value = Empty
txt_cartucho.Value = Empty
txt_cartuchomodelo.Value = Empty
combobox_cartuchofabricante.Value = Empty
combobox_cartuchotipo.Value = Empty
combobox_cartuchocor.Value = Empty
txt_cartuchocopias.Value = Empty
combobox_cartuchounidade.Value = Empty
txt_cartuchoimpressoras.Value = Empty
txt_cartuchovalorrecarga.Value = Empty
txt_cartuchovalorcompativel.Value = Empty
txt_cartuchovalorrevenda.Value = Empty
txt_enderecoimagem.Value = Empty
Imagem_cartucho.Picture = LoadPicture(Empty)

' Desabilitar campos

txt_cartucho.Locked = True
txt_cartuchomodelo.Locked = True
combobox_cartuchofabricante.Locked = True
combobox_cartuchotipo.Locked = True
combobox_cartuchocor.Locked = True
txt_cartuchocopias.Locked = True
combobox_cartuchounidade.Locked = True
txt_cartuchoimpressoras.Locked = True
txt_cartuchovalorrecarga.Locked = True
txt_cartuchovalorcompativel.Locked = True
txt_cartuchovalorrevenda.Locked = True
txt_cartucho.SetFocus
txt_enderecoimagem.Locked = True
btngravar_cartucho.Locked = True
btncancelar_cartucho.Locked = True



End If
End Sub

Lúcio Mauro da Silva

27-06-2016 23:00:17

Carlos boa noite,tudo bem?

O ideal é compartilhar seu arquivo para fazer as alterações nele.

Montei um exemplo, veja se te ajuda.

Valeu.

Abraço.

carloseg

27-06-2016 23:12:39

Lúcio, obrigado pela ajuda, vou tentar fazer com base no que me mandou senão der certo envio o arquivo.

Lúcio Mauro da Silva

27-06-2016 23:26:17

Blz...Carlos.

carloseg

28-06-2016 00:53:54

Lúcio Mauro da Silva, obrigado deu certinho. :)

Lúcio Mauro da Silva

28-06-2016 00:57:17

Show...legal.