PDA

View Full Version : Im stuck in VBA plz look code



Dan79
03-01-2015, 12:46 PM
im beginier in vba but practis :)
can any1 look code end say whot is wrong

Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtŠifra.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblLista(ID, datum, šifra, prezime, ime, prihod, prihod, uplate, ukupno, isplata, razlika) " & _
" VALUES(" & Me.txtdatum & ",'" & Me.txtŠifra & "','" & _
Me.txtPrezime & "','" & Me.txtIme & "','" & Me.txtPrihod & "','" & Me.txtPrihodi & "','" & _
Me.txtuplate & "','" & Me.txtUkupno & "','" & Me.txtIsplata & "','" & Me.txtrazlika & "')"

Else
'otherwise (Tag of txtsifra store the id of tblLista to be modified)
CurrentDb.Execute "UPDATE tblLista " & _
" SET šifra=" & Me.txtŠifra & _
", Datum='" & Me.txtdatum & "'" & _
", Prezime='" & Me.txtPrezime & "'" & _
", Ime='" & Me.txtIme & "'" & _
", prihodi='" & Me.txtPrihodi & "'" & _
", isplata='" & Me.txtIsplata & "'" & _
", prihod='" & Me.txtPrihod & "'" & _
", uplate='" & Me.txtuplate & "'" & _
" WHERE šifra=" & Me.txtŠifra.Tag
End If

jonh
03-01-2015, 01:47 PM
There's no 'end sub'.

Bob Phillips
03-02-2015, 01:25 AM
The one who can say what is wrong is you, so why don't you tell us? The best we can do is tell you why it is wrong.

jonh
03-02-2015, 02:52 AM
The fields in your insert don't match the values you are setting

You've listed the prihod field twice, the second one should be prihodi.

The ID field isn't given a value. If it's an autonumber you don't need to set it so leave the field out.

According to the UPDATE sql, datum is a text field so you need to put quotes around it's value like you did the others.

Dan79
03-02-2015, 11:42 AM
thx guys im done with tish problem, now work all fine

Dan79
03-02-2015, 11:44 AM
Const strcJetDate = "\#mm\/dd\/yyyy\#"


Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtŠifra.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblLista(datum, šifra, prezime, ime, prihod, prihodi, uplate, isplata) " & _
" VALUES('" & Me.txtdatum & "','" & Me.txtŠifra & "','" & _
Me.txtPrezime & "','" & Me.txtIme & "','" & Me.txtPrihod & "','" & Me.txtPrihodi & "','" & _
Me.txtuplate & "','" & Me.txtIsplata & "')"

Else
'otherwise (Tag of txtsifra store the id of tblLista to be modified)
CurrentDb.Execute "UPDATE tblLista " & _
" SET šifra=" & Me.txtŠifra & _
", Datum='" & Me.txtdatum & "'" & _
", Prezime='" & Me.txtPrezime & "'" & _
", Ime='" & Me.txtIme & "'" & _
", prihodi='" & Me.txtPrihodi & "'" & _
", isplata='" & Me.txtIsplata & "'" & _
", prihod='" & Me.txtPrihod & "'" & _
", uplate='" & Me.txtuplate & "'" & _
" WHERE šifra=" & Me.txtŠifra.Tag
End If

'clear form
cmdClear_Click
'refresh data in list on form
frmLista_subform.Form.Requery
End Sub

Private Sub cmdClear_Click()

Me.txtdatum = ""
Me.txtŠifra = ""
Me.txtPrezime = ""
Me.txtIme = ""
Me.txtPrihodi = ""
Me.txtPrihod = ""
Me.txtuplate = ""
Me.txtUkupno = ""
Me.txtIsplata = ""
Me.txtrazlika = ""


'focus on ID text box
Me.txtŠifra.SetFocus
'set button edit to enable
Me.cmdEdit.Enabled = True
'change caption of button add to Add
Me.cmdAdd.Caption = "Add"
'clear tag on šifra for reset new
Me.txtŠifra.Tag = ""
End Sub


Private Sub cmdClose_Click()
DoCmd.Close

End Sub

Private Sub cmdDelete_Click()

'delete record
'check existing selected record
If Not (Me.frmLista_subform.Form.Recordset.EOF And Me.frmLista_subform.Form.Recordset.BOF) Then
'confirm delete
If MsgBox("Are you sure to delete?", vbYesNo) = vbYes Then
'delete now
CurrentDb.Execute "DELETE FROM tblLista " & _
" WHERE šifra =" & Me.frmLista_subform.Form.Recordset.Fields("šifra")
'refresh data in list
Me.frmLista_subform.Form.Requery
End If
End If

End Sub

Private Sub cmdEdit_Click()

'check whether there exists data in list
If Not (Me.frmLista_subform.Form.Recordset.EOF And Me.frmLista_subform.Form.Recordset.BOF) Then
'get data to text box control
With Me.frmLista_subform.Form.Recordset
Me.txtdatum = .Fields("Datum")
Me.txtŠifra = .Fields("šifra")
Me.txtPrezime = .Fields("prezime")
Me.txtIme = .Fields("Ime")
Me.txtPrihodi = .Fields("Prihodi")
Me.txtPrihod = .Fields("Prihod")
Me.txtuplate = .Fields("uplate")
Me.txtUkupno = .Fields("ukupno")
Me.txtIsplata = .Fields("Isplata")
Me.txtrazlika = .Fields("razlika")
'store id of tblLista in Tag of txtšifra in case id is modified
Me.txtŠifra.Tag = .Fields("šifra")
'change caption of button add to Update
Me.cmdAdd.Caption = "Update"
'disable button edit
Me.cmdEdit.Enabled = False
End With
End If
End Sub