Cmd Click --- check the values first,append record and then delete them.

N

New_Access

Hi all,
I want to execute below code but it seems absolutely fail,
please help:

Private Sub AddSpec_cmd_Click()
On Error GoTo Err_AddSpec_cmd_Click

If IsNull(Me.Model) Then
MsgBox " Enter Model Name", vbOKOnly, "Model name empty"
End If
Model.SetFocus

If Inputvoltage.Value < 11 Then
MsgBox " Input correct voltage rate ", vbOKOnly, "Input voltage"
End If

Dim db As Database
Set db = CurrentDb

db.Execute "Appendix model spec1_qry", dbFailOnError
db.Execute "DELETE * FROM [Appendix model specification_tbl]",
dbFailOnError

Exit_AddSpec_cmd_Click:
Exit Sub

Err_AddSpec_cmd_Click:
MsgBox Err.Description

Resume Exit_AddSpec_cmd_Click:
End Sub

I want all condition must meet the criteria first
and then the execution is progressed.It seems
there will be many If..Then statement and I'm confuse
how to write a good code.

In fact,I want to add one more condition.
There are more 3 fields,if all of their values are 0,
the appending execution is fail
 
D

Dennis

What's wrong with it? Let me count the ways:

- You put your "Model.Setfocus" command outside the error block.
- You need to use (IMO) "Me.Model.Setfocus"
- You need to add the command EXIT SUB inside the error blobk
- DIM statements shoudl ALWAYS occur first in a Sub or Func (good coding
practice)

You've never really written code before in ANY language, have you? (Just
asking)
 
D

Douglas J. Steele

What I typically do is something like:

Private Sub AddSpec_cmd_Click()
On Error GoTo Err_AddSpec_cmd_Click

Dim db As Database
Dim strMessage As String

If IsNull(Me.Model) Then
strMessage = strMessage & _
" Enter Model Name" & vbCrLf
End If

If Inputvoltage.Value < 11 Then
strMessage = strMessage & _
" Input correct voltage rate " & vbCrLf
End If

If Len(strMessage) = 0 Then
Set db = CurrentDb
db.Execute "Appendix model spec1_qry", dbFailOnError
db.Execute "DELETE * FROM [Appendix model specification_tbl]", _
dbFailOnError
Else
MsgBox strMessage, vbOkOnly, "Errors Occurred"
End If

Exit_AddSpec_cmd_Click:
Exit Sub

Err_AddSpec_cmd_Click:
MsgBox Err.Description
Resume Exit_AddSpec_cmd_Click:

End Sub

Since focus can only be on a single control, it's (obviously) not possible
to set focus to all of the erroneous fields. You could keep choose to set
focus to, say, the first control that had an error.
 
N

New_Access

Thank's All,
Coding is a new thing for me so I always get stuck if I
have to use some code.
Can you advise some good resource for learn coding.



Douglas J. Steele said:
What I typically do is something like:

Private Sub AddSpec_cmd_Click()
On Error GoTo Err_AddSpec_cmd_Click

Dim db As Database
Dim strMessage As String

If IsNull(Me.Model) Then
strMessage = strMessage & _
" Enter Model Name" & vbCrLf
End If

If Inputvoltage.Value < 11 Then
strMessage = strMessage & _
" Input correct voltage rate " & vbCrLf
End If

If Len(strMessage) = 0 Then
Set db = CurrentDb
db.Execute "Appendix model spec1_qry", dbFailOnError
db.Execute "DELETE * FROM [Appendix model specification_tbl]", _
dbFailOnError
Else
MsgBox strMessage, vbOkOnly, "Errors Occurred"
End If

Exit_AddSpec_cmd_Click:
Exit Sub

Err_AddSpec_cmd_Click:
MsgBox Err.Description
Resume Exit_AddSpec_cmd_Click:

End Sub

Since focus can only be on a single control, it's (obviously) not possible
to set focus to all of the erroneous fields. You could keep choose to set
focus to, say, the first control that had an error.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


New_Access said:
Hi all,
I want to execute below code but it seems absolutely fail,
please help:

Private Sub AddSpec_cmd_Click()
On Error GoTo Err_AddSpec_cmd_Click

If IsNull(Me.Model) Then
MsgBox " Enter Model Name", vbOKOnly, "Model name empty"
End If
Model.SetFocus

If Inputvoltage.Value < 11 Then
MsgBox " Input correct voltage rate ", vbOKOnly, "Input voltage"
End If

Dim db As Database
Set db = CurrentDb

db.Execute "Appendix model spec1_qry", dbFailOnError
db.Execute "DELETE * FROM [Appendix model specification_tbl]",
dbFailOnError

Exit_AddSpec_cmd_Click:
Exit Sub

Err_AddSpec_cmd_Click:
MsgBox Err.Description

Resume Exit_AddSpec_cmd_Click:
End Sub

I want all condition must meet the criteria first
and then the execution is progressed.It seems
there will be many If..Then statement and I'm confuse
how to write a good code.

In fact,I want to add one more condition.
There are more 3 fields,if all of their values are 0,
the appending execution is fail
 

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