Results 1 to 12 of 12

Thread: [RESOLVED] update databse sql every records

  1. #1

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Resolved [RESOLVED] update databse sql every records

    Hi
    This is the code that I write to update database sql. When user type a
    number in text1.text press enter, he will open TABEL1 and check record
    from first to last to chech field(0), if exist text2 get value field(1)
    and value in field(1) is deduted by 1 and update to the TABEL1

    The fact here when runtime, every record in the TABEL1 are deducted when
    number exist;text1.text=field(0)

    When I use change the connectionstring and connect to Acces tabel, it
    run smoothly;only 1 record is updated.

    If any one could give an idea of how it would work be greatly appreciated
    and thanks a lot
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.  
    3. If KeyAscii = 13 Then
    4.     Label3.Visible = False
    5.     On Error GoTo errhandler 'on error resume next
    6.     Set objConnection = New ADODB.Connection
    7.     objConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=adodata;Data Source=pc14;"
    8.     objConnection.CursorLocation = adUseClient
    9.     objConnection.Open
    10.     Set objRS = New ADODB.Recordset
    11.  
    12. objRS.Open "SELECT * FROM TABLE1", objConnection, adOpenDynamic, adLockOptimistic
    13.    
    14.     objRS.MoveFirst
    15.     Do While Not objRS.EOF
    16.         If Text1.Text = objRS.Fields(0) Then
    17.            
    18.             Text2.Text = objRS.Fields(1)
    19.             If Text2.Text <= 1 Then
    20.                 Text1.Text = ""
    21.                 Text2.Text = ""
    22.                 objConnection.Close
    23.                 Label3.Caption = "Sorry! Code expired"
    24.                 Label3.Visible = True
    25.                 Exit Sub
    26.             Else
    27.                 lngSetTop = SetTopMost(hWnd, False)
    28.                 Timer1.Enabled = True
    29.                 objRS.Fields(1) = objRS.Fields(1) - 1
    30.                 objRS.Update
    31.                 DisableCtrlAltDelete (True)
    32.                 Form1.Show
    33.                 AdoDao.Visible = False
    34.                 Exit Do
    35.             End If
    36.        
    37.         End If
    38.         objRS.MoveNext
    39.         Loop
    40.      
    41. End If
    42. errhandler:
    43. If Err.Number = 2147467259 Then
    44.     objConnection.Close
    45.     AdoDao.Show
    46.         Label3.Caption = "Server fail"
    47.         Label3.Visible = True
    48.   Exit Sub
    49. End If
    50. End Sub

  2. #2
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: update databse sql every records

    I don't why that happens, but why don't use a SQL update statement, something like:

    VB Code:
    1. objconnection.Execute "update table1 set Field1 =  field1-1 where field2 = " & val(me.text1)

  3. #3

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    objconnection.Execute "update table1 set Field1 = field1-1 where field2 = " & val(me.text1)
    this code should be "update FROM table1 "
    but it is not doing anything to the records, anyway thanks for response

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: update databse sql every records

    In MS SQL Server...

    UPDATE TABLE SET COLUMN=NEWVALUE WHERE CONDITION=SOMETHING

    ...is valid

    Your WHERE clause must not be finding anything.

    And the suggestion to use an ACTION query .EXECUTE'd against the DB is a good one. Using the RS to UPDATE the table is not required.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    The update is working but the every records here is around the do...loop and movenext instability. Any comments are welcome.

    thanks for you all

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: update databse sql every records

    Quote Originally Posted by ksuwanto8ksd
    The update is working but the every records here is around the do...loop and movenext instability. Any comments are welcome.

    thanks for you all
    As I also said in ur last post, after a Connection.Execute, always Requery the recordsets that were connected to the tables affected by that Execute statement. This will ensure that ur recordsets now have the correct data.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    hhaa
    Last edited by ksuwanto8ksd; Jun 13th, 2005 at 06:16 AM.

  8. #8

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    How Do I make the above code working in SQL database?
    the code will run on ACCESS database smootly.
    Thank If you have idea

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: update databse sql every records

    I think there is a general consensus here on the forum that using a DB like SQL server means abandoning old concepts of iterating through records and processing - not using BOUND controls...

    SQL Server is much different than ACCESS - it's a true client/server database. We do all our business processing in STORED PROCEDURES, written in T-SQL, on the SERVER - not in the client.

    As much as is possible we do it using "set-based logic" - creating temp tables or table variables - using those to perform mass UPDATE's to rows. Occasionaly we fall back on using a CURSOR in the SPROC - but that is always avoided if possible.

    In three years of developing major applications with VB and MS SQL, our VB front-end has remained very static, and it calls over 1000 STORED PROCEDURES to perform the data operations.

    When we get a RECORDSET back from a SPROC we load it into a VB control (such as a flexgrid) and then lose the RECORDSET. Any updates back to the table are done one row at a time - using an UPDATE SPROC.

    That's our philosophy anyway...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    What is a UPDATE SPROC?

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: update databse sql every records

    Quote Originally Posted by ksuwanto8ksd
    What is a UPDATE SPROC?
    Here is an example of one. It's a STORED PROCEDURE - becomes part of the DATABASE itself. Executing this SCRIPT in QUERY ANALYZER loads the SPROC into the database. It's called using ADO from the VB side - passing arguments and getting returned values.

    Code:
    Create Procedure frmTeaAttend_ClsAttendanceClosed_T_Post 
    @Retstat int OUTPUT, @Rettext varchar(50) OUTPUT, @StuID int, @Yr int,  
    @Bldg tinyint, @Crs char(4), @Sectn tinyint, @TimeVec tinyint, 
    @ClsAttDate datetime, @ClsAttValue money, @ClsAttClosed varchar(3), @ResetLetter char(1)
    as
    
    DECLARE @Rollback int
    DECLARE @Count int
    DECLARE @LtrStat char(1)
    
    
    BEGIN TRAN
    
    Set @LtrStat = 'N'
    
    If @ClsAttClosed = 'C'
    Begin
    	Set @LtrStat = 'V'
    End
    
    If @ResetLetter = '1'
    Begin
    	Set @LtrStat = 'N'
    End
    
    UPDATE CLSATTENDANCE_T SET CLSATTCLOSED = @ClsAttClosed, CLSATTVALUE = @ClsAttValue, TDate = GetDate()
    	WHERE (STUID = @StuId AND YR = @Yr AND BLDG = @Bldg AND CRS = @Crs AND 
    	SECTN = @Sectn AND TIMEVEC = @TimeVec AND CLSATTDATE = @ClsAttDate)
    
    SELECT @Rollback = @@error
    
    If @RollBack = 0
    BEGIN
    
    	SET @Count = (SELECT COUNT(*) FROM Letter_T 
    		WHERE Stuid = @Stuid AND Yr = @Yr AND Bldg = @Bldg
    		AND Crs = @Crs AND Sectn = @Sectn AND TimeVec = @TimeVec
    		AND LtrDate = @ClsAttDate AND LtrType = '01')
    		
    	If @Count < 1 AND @ClsAttClosed = 'C'
    	Begin
    		INSERT INTO Letter_T Values (@StuId, @Yr, @Bldg, @Crs, @Sectn, @TimeVec, @ClsAttDate, '01', 'V', GetDate(), '', GetDate())
    		SELECT @Rollback = @@error
    	End
    
    	IF @Count > 0
    	BEGIN
    		UPDATE Letter_T Set LtrType = '01', LtrStatDate = GetDate(), LtrStat = @LtrStat
    			WHERE Stuid = @Stuid AND Yr = @Yr AND Bldg = @Bldg
    			AND Crs = @Crs AND Sectn = @Sectn AND TimeVec = @TimeVec
    			AND LtrDate = @ClsAttDate AND LtrType = '01'
    		SELECT @Rollback = @@error
    	END
    
    END
    
    IF @Rollback <> 0
    BEGIN
    	ROLLBACK TRAN
    	SELECT @Retstat = -1
    	SELECT @Rettext = 'Failure'
    END
    ELSE
    BEGIN
    	COMMIT TRAN
    	SELECT @Retstat = 0
    	SELECT @Rettext = 'Success'
    END
    
    SELECT @Retstat, @Rettext
    
    GO
    Here's another example of one:

    Code:
    Create Procedure frmTeaAttend_ClsPosted_T_Post 
    @Retstat int OUTPUT, @Rettext varchar(50) OUTPUT, @Yr int,  
    @Bldg tinyint, @Crs char(4), @Sectn tinyint, @TimeVec tinyint, 
    @ClsPostDate datetime, @ClsPostStat char(1), @TDate DateTime
    as
    
    DECLARE @Rollback int
    DECLARE @Count int
    
    BEGIN TRAN
    
    SELECT @Count = (SELECT COUNT(*) From ClsPosted_T WHERE YR = @Yr AND BLDG = @Bldg AND CRS = @Crs AND SECTN = @Sectn
            AND TIMEVEC = @TimeVec AND ClsPostDate = @ClsPostDate)
    
    IF @Count < 1
    BEGIN
    	INSERT INTO ClsPosted_T VALUES (@Yr, @Bldg, @Crs, @Sectn, @TimeVec,
    		@ClsPostDate, @ClsPostStat, @TDAte)
    
    	SELECT @RollBack = @@error
    END
    IF @Count > 0
    BEGIN
    
    	UPDATE ClsPosted_T SET ClsPostStat = @ClsPostStat, TDate = @TDate  
    	WHERE YR = @Yr AND BLDG = @Bldg AND CRS = @Crs AND SECTN = @Sectn
            AND TIMEVEC = @TimeVec AND ClsPostDate = @ClsPostDate
    	SELECT @Rollback = @@error
    
    END
    
    IF @Rollback <> 0
    BEGIN
    	ROLLBACK TRAN
    	SELECT @Retstat = -1
    	SELECT @Rettext = 'Failure'
    END
    ELSE
    BEGIN
    	COMMIT TRAN
    	SELECT @Retstat = 0
    	SELECT @Rettext = 'Success'
    END
    
    SELECT @Retstat, @Rettext
    These are very old - from our early days - I certainly don't like some of what we are doing in these SPROCS - but they will server as examples.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: update databse sql every records

    I'm newbie, I'm now begin with the "update add edit insert delete" records
    But your code maybe usefull someday, I will save it for future refference.
    thanks and is appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width