By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Save button using toolbar

100+
P: 143
Hello everyone,
I m using some button using toolbar such as Add,Save,View,.... my save button is not working.... it doesnt give me any error but does not save to my database.... or showing in my gird... Plz Help
here is my code for save button
Expand|Select|Wrap|Line Numbers
  1. Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
  2. On Error GoTo ChkErr
  3.     If Button.Index = 1 Then
  4.             Mode = "Add"
  5.             Call AddMode
  6.             Call ClearControl
  7.             Toolbar1.Buttons.Item(1).Enabled = False ' Add
  8.             Toolbar1.Buttons.Item(2).Enabled = False ' View
  9.             Toolbar1.Buttons.Item(3).Enabled = False ' Sort
  10.             Toolbar1.Buttons.Item(4).Enabled = False ' Edit
  11.             Toolbar1.Buttons.Item(5).Enabled = True ' save
  12.             Toolbar1.Buttons.Item(9).Enabled = True 'Exit
  13.  
  14.               End If
  15.  
  16.     If Button.Index = 4 Then
  17.         'If Val(MSHFlexGrid1.TextMatrix(MSHFlexGrid1.RowSel, 8)) = "0" Then
  18.             Mode = "Edit"
  19.             Toolbar1.Buttons.Item(5).Enabled = True ' save buton
  20.               Call Enable
  21.         Else
  22.             MsgBox " Cannot Edit "
  23.         End If
  24.  
  25.     If Button.Index = 5 Then
  26.  
  27.             Call SaveMode
  28.             Call ClearControl
  29.             Call Desable
  30.             MsgBox " Record Saved For Id '" & txtId.Text & "'"
  31.             Toolbar1.Buttons.Item(5).Enabled = False
  32.             Toolbar1.Buttons.Item(1).Enabled = True ' Add
  33.  
  34.             Call Address
  35.     End If
  36.  
  37.     'End If
  38.  
  39.     If Button.Index = 2 Then ' View
  40.         'Toolbar1.Buttons.Item(7).Enabled = False ' Final
  41.         Toolbar1.Buttons.Item(1).Enabled = False ' Add
  42.         Toolbar1.Buttons.Item(4).Enabled = True ' Edit
  43.  
  44.     End If
  45.  
  46.  
  47.  
  48.     If Button.Index = 7 Then ' Cancel
  49.             Db.Execute "Update Address"
  50.             Call Address
  51.  
  52.     End If
  53.  
  54.  
  55.     If Button.Index = 9 Then ' Exit
  56.             Unload Me
  57.     End If
  58.  
  59. Exit Sub
  60. ChkErr:
  61.     If Err.Number > 0 Then
  62.     MsgBox Err.Number & vbNewLine & Err.Description
  63.                Call ErrHand(Me.Caption, "cmdSave_Click")
  64.  
  65.     End If
  66. End Sub
  67.  
  68.  
  69. Public Function SaveMode()
  70. 'On Public Function SaveMode()
  71. If Mode = "Add" Then
  72.     ' To Get ReqNo from Mslastdoc
  73.     Dim Rs As New ADODB.Recordset
  74.         If Rs.State = adStateOpen Then Rs.Close
  75.          Rs.Open ("SELECT * FROM MsLastDoc where YY ='" & FinancialYear & "'"), Db, adOpenKeyset, adLockPessimistic
  76.                   txtId.Text = Rs("LastDocNo").Value + 1
  77.              Rs.Close
  78.          ' End
  79.  
  80.     ' Save LastDoc
  81.     Dim LastDoc As New ADODB.Recordset
  82.     Dim DocNo As Integer
  83.                 DocNo = Val(txtId.Text)
  84.                 If LastDoc.State = adStateOpen Then LastDoc.Close
  85.                 LastDoc.Open ("Select * From MsLastDoc where YY ='" & FinancialYear & "' "), Db, adOpenKeyset, adLockPessimistic
  86.                 Db.Execute "Update MsLastDoc set LastDocNo ='" & DocNo & "' where YY ='" & FinancialYear & "' "
  87.     ' End Save
  88. End If
  89.  
  90. Call SaveAddress
  91. Call ClearControl
  92.  
  93. End Function
  94. Public Function SaveAddress()
  95.     Db.Execute " Delete from Adress where Id = '" & txtId.Text & "' "
  96.  
  97.         Dim RsAddress As New ADODB.Recordset
  98.        'If RsReqHdr.State = adStateOpen Then RsReqHdr.Close
  99.         RsAddress.Open ("Address"), Db, adOpenKeyset, adLockPessimistic
  100.         With RsAddress
  101.         RsAddress.AddNew
  102.                 .Fields("Id") = Val(txtId.Text)
  103.                 .Fields("Addressof") = Val(cmbadrof.Text)
  104.                 .Fields("Address") = Val(txtAddress.Text)
  105.                 .Fields("city") = Val(txtCity.Text)
  106.                 .Fields("Pin") = Val(txtPin.Text)
  107.                 .Fields("entrydt") = Format(DTPentrydt, "DD-MMM-YYYY")
  108.                 .Fields("closedt") = Format(DTPClosedt, "DD-MMM-YYYY")
  109.  
  110.             .Update
  111.                 End With
  112.             RsAddress.Close
  113. End Function
  114.  
Jun 16 '08 #1
Share this Question
Share on Google+
14 Replies


Dököll
Expert 100+
P: 2,364
Greetings, squrel!

I am not sure what the problem is, are you having problems with just your save mechanism? Did you know you can run the code in debug mode to follow what is happening. Please write if you need to know how to do that.

Do stay tuned for a better answer on this one, like I said I am not certain how this could go.

In a bit!

Dököll
Jun 17 '08 #2

100+
P: 143
plz tel me how to run it in a dubug mode... and non of my button is working... dont knw why? thank u
Jun 17 '08 #3

QVeen72
Expert 100+
P: 1,445
Hi,

First Remove the error Handling and Run the Code.
Keep a BreakPoint on First Line of the Code.
and Check the Run-Sequence..

Regards
Veena
Jun 17 '08 #4

100+
P: 143
Hi... i have changed the code to this but still i have error in this line :

'txtAddressId.Text = GetNextCode("Address", "AdrID")


why my getnextcode is not working? it gives me the error of Sub or function not defined? i dont get wht is the problem.... plz help... thankx



Option Explicit

Private Sub Form_Initialize()
On Error GoTo ChkErr
Db.Close
Db.ConnectionString = pStrConnectionString
Db.ConnectionTimeout = 0
Db.Open
Db.CursorLocation = adUseClient
DoEvents
Exit Sub
ChkErr:
If Err.Number > 0 Then
MsgBox Err.Number & vbNewLine & Err.Description
Call ErrHand(Me.Caption, "Form_Initialize")
'Resume Next
End If
End Sub
Private Sub Form_Load()
On Error GoTo ChkErr
FormUpdateSQL1.ConnectionString = pStrConnectionString
FormUpdateSQL1.TableName = "Address"
FormUpdateSQL1.FieldList = "AdrId;AdrOf;Address;City;Pin;Entrydt;Closedt;Ind; "
FormUpdateSQL1.ControlList = "txtId;cmbadrof;txtAddress;txtCity;txtPin;DTPentry dt;DTPclosedt;ChkInd;"
FormUpdateSQL1.ViewFields = "AdrId;AdrOf;Address;City;Pin;Entrydt;Closedt;Ind; "
FormUpdateSQL1.FormatString = "AdrId;AdrOf;Address;City;Pin;Entrydt;Closedt;Ind; "
FormUpdateSQL1.DoVisible 4, False
FormUpdateSQL1.DoVisible 6, False
FormUpdateSQL1.DoVisible 8, False
FormUpdateSQL1.AllowEdit = True
FormUpdateSQL1.Refresh
cmbadrof.Enabled = False
Call Grid_Data
Exit Sub
ChkErr:
If Err.Number > 0 Then
MsgBox Err.Number & vbNewLine & Err.Description
Call ErrHand("Address", "Form_Load")
'Resume Next
End If

Grid_Data
End Sub
Public Sub Grid_Data()
On Error GoTo ChkErr
Adodc1.ConnectionString = pStrConnectionString
Adodc1.RecordSource = "Select AdrID as ID,Adrof,Address,City,Pin,entrydt as EntryDate,closedt as CloseDate from Address"
Adodc1.Refresh
MSHFlexGrid1.ColWidth(1) = 800
MSHFlexGrid1.ColWidth(2) = 1000
MSHFlexGrid1.ColWidth(3) = 2000
MSHFlexGrid1.ColWidth(4) = 1000
MSHFlexGrid1.ColWidth(5) = 800
MSHFlexGrid1.ColWidth(6) = 1000
MSHFlexGrid1.ColWidth(7) = 2000
MSHFlexGrid1.Refresh
Exit Sub
ChkErr:
If Err.Number > 0 Then
MsgBox Err.Number & vbNewLine & Err.Description
Call ErrHand("Address", "Grid_Data")
'Resume Next
End If
End Sub

Private Sub FormUpdateSQL1_AddClick()
cmbadrof.Enabled = True
'txtAddressId.Text = GetNextCode("Address", "AdrID")
cmbadrof.Enabled = True
ChkInd.Value = 1
End Sub

Private Sub FormUpdateSQL1_AfterControlsEnabled()
cmbadrof.Enabled = True
End Sub
Private Sub FormUpdateSQL1_AfterUpdate()
cmbadrof.Enabled = False
Call Grid_Data
End Sub
Private Sub FormUpdateSQL1_BeforeValidate()
If cmbadrof.Text = "" Then
MsgBox " Enter the selected address"
cmbadrof.SetFocus
Exit Sub
FormUpdateSQL1.CancelEvent = True
End If
End Sub
Private Sub MSHFlexGrid1_DblClick()
FormUpdateSQL1.ViewRecord True, Val(MSHFlexGrid1.Text)
End Sub
Jun 18 '08 #5

100+
P: 143
Is anyone can help me in this matter??
Jun 18 '08 #6

QVeen72
Expert 100+
P: 1,445

txtAddressId.Text = GetNextCode("Address", "AdrID")
This looks like you are calling a Function...
and you may not have that Function in your code, and the Error is the same...

Regards
Veena
Jun 18 '08 #7

100+
P: 143
Hi
In this code how can i get the next Id?? I mean if i have 7 record saved in this, when i press the add button i want the no 8 appears in my txtId... Can anyone help me plz... thankx a lot
Jun 19 '08 #8

100+
P: 143
Hi Guys..
I need some help here if anyone can.. thankx
Jun 20 '08 #9

QVeen72
Expert 100+
P: 1,445
Hi,

Write this Function :
{AIRCODE}

Expand|Select|Wrap|Line Numbers
  1. Private Function MyNextID() As Long
  2. Dim RSI As New ADODB.RecordSet
  3. MyNextID = 0
  4. RSI.Open "Select Max(ID) From MyTable",Conn
  5. If Not RSI.EOF Then
  6.     RSI.MoveFirst
  7.     MyNextID = Val(RSI(0) & "")
  8. End If
  9. MyNextID = MyNextID + 1
  10. RSI.Close
  11. Set RSI = Nothing
  12. End Function
  13.  
Call the above Function, In cmdADD_Click
Expand|Select|Wrap|Line Numbers
  1. txtID.Text = MyNextID
  2.  
Regards
Veena
Jun 20 '08 #10

100+
P: 143
Hi Veena..

That Code is not working in my project... my add function is this

Private Sub FormUpdateSQL1_AddClick()

cmbadrfor.Enabled = True
'txtId.Text = GetNextCode("Address", "AdrID")
cmbadrfor.Enabled = True
txtId.Enabled = False
ChkInd.Value = 1
End Sub

tht part of the code is not working in my function... wht else i can do..
thanks a lot
Jun 21 '08 #11

QVeen72
Expert 100+
P: 1,445
Hi,

you are using VB6 or VBA....?

My code is for VB6

Regards
Veena
Jun 21 '08 #12

100+
P: 143
I m using VB6 with SQLserver 2000
Jun 21 '08 #13

QVeen72
Expert 100+
P: 1,445
Hi,

Did you add the Function what I said in My previous post..?
Jun 22 '08 #14

100+
P: 143
Yes i added tht function the same way u told me but it is not working and still getting error
Jun 23 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.