Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting

Post new topic   Reply to topic
help with database update
View previous topic :: View next topic  
Author Message
KaleKYlssen
Newbie


Joined: 31 Jan 2004
Posts: 19

PostPosted: Feb 8th, 2004 08:06 AM    Post subject: help with database update Reply with quote

so far i have this to update a existing record and it works,

Code:

Private Sub cboStaff_Click()
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\Elco.mdb"
sqlefficiency = "SELECT * FROM Efficiency WHERE  StaffID = '" & cboStaff.Text & "' AND WeekStarting = #" & cboWeek.Text & "#"

rsEfficiency.Open sqlefficiency, ConnString, adOpenKeyset, adLockOptimistic, adCmdText
End Sub

Private Sub cmdExit_Click()
Unload Me
frmMenu.Show
End Sub

Private Sub cmdUpdate_Click()
Dim IntUn As Integer
Dim IntAs As Integer
rsStock.Open
With rsStock
    IntUn = CInt(rsStock.Fields("Assembled Stock"))
    IntAs = CInt(rsStock.Fields("Unassembled Stock"))
    rsStock.Fields("Assembled Stock") = IntUn + txtAmountMade.Text
    rsStock.Fields("Unassembled Stock") = IntAs - txtAmountMade.Text
    rsStock.Update
End With
rsStock.Close

With rsEfficiency
    IntTime = CInt(rsEfficiency.Fields("TimeTaken"))
    IntMotor = CInt(rsEfficiency.Fields("MotorsMade"))
    .Fields("TimeTaken") = IntTime + txtTimeTaken.Text
    .Fields("MotorsMade") = IntMotor + Val(txtAmountMade.Text)
    .Update
    .Close
End With

txtTimeTaken.Text = ""
txtAmountMade.Text = ""
End Sub


what i want to be able to do is when i enter a compund key that doesn't exist in the database, a new record will be added. To do this will i need to use the NoMatch function of sql,
any help appreciated
Back to top
View user's profile Send private message Send e-mail
Andir
Centurion


Joined: 21 Dec 2003
Posts: 184
Location: Chicago Area

PostPosted: Feb 9th, 2004 12:20 AM    Post subject: Reply with quote

I'm not sure that it would work, since I have not tried it personally, but you could attempt to to a database Execute. You connecting to the database in a similar way to ADO's connection/recordset method. With ADO, I usually declare a Database connection:
Private DB_something as ADODB.Connection
Private RS_something as ADODB.Recordset

and I use that to open the database:
DB_something.Open <connectionstring>, {user}, {pass}
then pull the recordset from that connection:
RS_something.Open <SQL Query>, DB_something, {additional record type flags, etc.}
You can use the DB connection object to run execution statements. I think you can use this to run stored procedures and such. MSDN states:
"Executes the specified query, SQL statement, stored procedure, or provider-specific text."
The format is as follows:
connection.Execute CommandText, {RecordsAffected}, {Options}

This returns a recordset of data if there is a return. Technically you can run your SQL statements this way.

Does that help any?
_________________
If you happen to see little people sitting on your desk...don't tell anyone or they might think your crazy too.
Back to top
View user's profile Send private message
KaleKYlssen
Newbie


Joined: 31 Jan 2004
Posts: 19

PostPosted: Feb 9th, 2004 02:17 PM    Post subject: Reply with quote

i may be able to do it that way but i don't want to change to mush of the coding, does any know is its posible to link a sql statement with an If statement and do it that way.
Thanks for your suggestion Andir Wink
Back to top
View user's profile Send private message Send e-mail
KaleKYlssen
Newbie


Joined: 31 Jan 2004
Posts: 19

PostPosted: Feb 15th, 2004 09:41 AM    Post subject: Reply with quote

i'm still having trouble with this, haven't been able to find a way to add a new record if the sql statement doesn't return a record can anyone help
Back to top
View user's profile Send private message Send e-mail
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Feb 15th, 2004 09:46 AM    Post subject: Reply with quote

Hi!

So you're trying to add a new record if no record is returned by the sql query, right ?

If yes, then better use the RecordCount property to get how many records have been returned. If greater than 0 then add new record else loop and show records.

Code:
If rsEfficiency.RecordCount = 0 Then 'Code for new records


I hope this solves your problem!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
KaleKYlssen
Newbie


Joined: 31 Jan 2004
Posts: 19

PostPosted: Feb 15th, 2004 10:18 AM    Post subject: Reply with quote

WOW your a genius ( compared to me anyway), thats solved my problem thank you, iv'e been stuck on that for a while =D
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic