borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Need help creating database, a newbie)Next Topic (DAO connecting ODBC) New Topic New Poll Post Reply
AndreaVB Forum : Database : Add Command not working
Poster Message
Nabeel
Level: Big Cheese

Registered: 22-02-2005
Posts: 22

icon Add Command not working

BELOW IS CODE PROBLEM IS ADD CMD IS NOT WORKING CAN SOME CORRECT IT
Dim cnMainData As New Connection
Dim bEdit, bAddNew As Variant

Dim rsProducts As New Recordset


Private Sub cmdAdd_Click()
bEdit = True
bAddNew = True
End Sub

Private Sub cmdCancel_Click()
ShowData
bEdit = False
bAddNew = False
SetDisplayMode
End Sub

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdFirst_Click()
rsProducts.MoveFirst
ShowData
End Sub

Private Sub cmdLast_Click()
rsProducts.MoveLast
ShowData
End Sub

Private Sub cmdNext_Click()
With rsProducts
.MoveNext
If .EOF Then .MoveLast
End With
ShowData
End Sub


Private Sub cmdPrevious_Click()
With rsProducts
.MovePrevious
If .BOF Then .MoveFirst
End With
ShowData

cmdPrevious.Visible = False
cmdNext.Visible = False
cmdLast.Visible = False
cmdSave.Visible = True
cmdCancel.Visible = True
End Sub
Private Sub cmdSave_Click()
With rsProducts
If bAddNew Then .AddNew
!ProductName = txtProdName.Text
!UnitPrice = Val(txtUnitPrice.Text)
!unitsInStock = Val(txtInStock.Text)
!unitsOnOrder = Val(txtOnOrder.Text)
.Update
End With
End Sub


Private Sub Form_Activate()
ShowData
End Sub

Private Sub Form_Load()
With cnMainData
.Provider = \"Microsoft.jet.OLEDB.3.51\"
.ConnectionString = \"D:\\Program Files\\Microsoft Visual Studio\\VB98\\Nwind.mdb\"
.Open
End With
With rsProducts
    .Open \"Products\", cnMainData, adOpenKeyset, adLockBatchOptimistic
    End With
bEdit = False
bAddNew = False


End Sub

Public Sub ShowData()
txtProdName.Text = rsProducts!ProductName & \"\"
txtUnitPrice.Text = rsProducts!UnitPrice
txtInStock.Text = rsProducts!unitsInStock
txtOnOrder.Text = rsProducts!unitsOnOrder
txtProdName.DataChanged = False
txtUnitPrice.DataChanged = False
txtInStock.DataChanged = False
txtOnOrder.DataChanged = False
End Sub

Public Sub SetEditMode()
cmdFirst.Visible = False
cmdPrevious.Visible = False
cmdNext.Visible = False
cmdLast.Visible = False
cmdSave.Visible = True
cmdCancel.Visible = False
End Sub

Public Sub SetDisplayMode()
cmdFirst.Visible = True
cmdPrevious.Visible = True
cmdNext.Visible = True
cmdLast.Visible = True
cmdSave.Visible = False
cmdCancel.Visible = True
End Sub

Private Sub txtInStock_Change()
bEdit = True
SetEditMode
End Sub

Private Sub txtOnOrder_Change()
bEdit = True
SetEditMode
End Sub

Private Sub txtProdName_Change()
bEdit = True
SetEditMode
End Sub

Private Sub txtUnitPrice_Change()
bEdit = True
SetEditMode
End Sub


____________________________
Nabeel Ahmad

03-03-2005 at 08:57 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Add Command not working

Do you mean this add command

quote:
Private Sub cmdAdd_Click()
bEdit = True
bAddNew = True
End Sub



What are you expecting it to do , your only setting two booleans to true.

Steve  
04-03-2005 at 09:00 AM
View Profile Send Email to User Show All Posts | Quote Reply
Nabeel
Level: Big Cheese

Registered: 22-02-2005
Posts: 22
icon Re: Add Command not working

that is why i am asking for help what else is required to complete this project as i am unable to add any thing
in assingment it was Written as below
IN THE CLICK EVENT OF THE cmdAdd BUTTON,ADD CODE TO CLEAR ALL THE TEXT BOXEX AND SET THE EDIT FLAG(bEdit) AND ADD RECORD FLAG(bAddNew) TO TRUE

in response to this that what code i have written can you help

____________________________
Nabeel Ahmad

04-03-2005 at 10:04 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Add Command not working

You miss a Confirm button.
It seems to me that there is a misunderstanding of the Add/Edit database procedure.

When you add a record, the steps are:
0 - at the beginning, the db is in some locked status, to prevent accidental editing of data
1 - entering in Edit mode: the db is notified of the adding attempt ("...Hey, db, I'm about to add a new record..."), so the engine 'unlocks' it
2 - A new line is created at the end of the recordset, meaning the empty fields are prepared to be filled with the values of the new record (see note B below)
3 - Pause. At this point the engine stops. There is nothing more to do unless the user has filled the fields.
4 - When the user has finished entering the values (How can we know it? See note A below) the db has to save the new record  with the new values, and then lock again the db. The Edit mode is finished. When you edit a record instead of adding, the only difference is that there is no new line added.

So, even if some dbms make all this unlock/adding/lock process without the user can see it, this mechanism has to be taken in mind. For instance, ADO is transparent to the user during the process, but if you want to walk you own way, you must take care of these steps to do. So you have to manually
UNLOCK
ADD (an empty row)
FILL (the row)
UPDATE
LOCK (again)

Even using some ADO controls you can do your operation manually, but in this case some steps can be grouped. The only step that is really separated by others is the filling phase, because is not you (the program) who do it, but the user, so we can't really know when he's finished.
A) This is the reason because many applications provide a Confirm/Cancel couple of buttons: after insertion the user has to notify the db he's finished, so the db engine can start running again to save or discard changes, and lock the recordset again. Many db apps show the editing tools disabled during the locked status, and enable them only after the user has explicitly declared his intentions for Add/Edit operations, by clicking in an Add or an Edit button. And also the navigation commands are usually disabled during the edit status, as ADO automatically updates changes and exit the Edit status when moving to another record. You already did it.

So finally the user can Confirm or Cancel the changes to the new record. But if he choose to Cancel, the new record has to be deleted. Sometimes this can cause some troubles with indexes.
B) An alternative way is not to create any new empty row, but keeping the new values into the memory. Clicking the Add button just disconnects the textboxes from the recordset and clear them: only at the end, if the user clicks on Confirm, I can effectively unlock, create a new row in the recordset, fill it, save it and lock again. And relink the textboxes.
This is the way I use.

So I say:
- it has to happen nothing when you click the cmdAdd button, just clear out the textboxes;
- when clicking on it, disable all the navigation and editing controls. Also disconnect them from the recordset;
- provide a Confirm and a Cancel button: clicking on Cancel just clear the textboxes and reconnect them to the recordset. If Confirm is clicked you do all the steps above, then reconnect the textboxes and move the recordset to the end (the new added record).

You already do many of these things, but you miss a Confirm button, and I would put the complete code to add the new record in it.

Check if you have to explicitly call the Update method or not. I spoke about ADO for instance, but you have to check the features of the provider you use.

Hope it's clear and helpful.

____________________________
Real Programmer can count up to 1024 on his fingers

04-03-2005 at 12:24 PM
View Profile Send Email to User Show All Posts | Quote Reply
Nabeel
Level: Big Cheese

Registered: 22-02-2005
Posts: 22
icon Re: Add Command not working

Dear sir, thanks for the help it is working now

____________________________
Nabeel Ahmad

04-03-2005 at 06:42 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : Add Command not working
Previous Topic (Need help creating database, a newbie)Next Topic (DAO connecting ODBC) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder