Results 1 to 4 of 4

Thread: Convert VB6.0 code to Stored Procedure

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow Convert VB6.0 code to Stored Procedure

    Paging SZLAMANY and others! I wish to convert the following code but I don't know where to start...
    VB Code:
    1. 'Function:
    2.     'To add values to Expenditure field wherein DateEarned field has elapsed for a month,
    3.     'the expenditure value will be deducted from the TotalCost which should be deducted
    4.     'from the Earnings table to reflect the real income for the previous months
    5. Private Sub Command2_Click()
    6.     Dim adoRecordset    As ADODB.Recordset
    7.     Dim adoRecordset2   As ADODB.Recordset
    8.     Dim Total           As Currency
    9.     Dim TotalFrameCost  As Currency
    10.     Dim curExpenditure  As Currency
    11.     Dim curEyeglass     As Currency
    12.     Dim strData         As String
    13.     Dim strSQL          As String
    14.     Dim PurchaseCode    As Long
    15.    
    16.     'Clear previous automated calculations:
    17.     strSQL = "UPDATE tabEarningRecords " & _
    18.              "SET Expenditure = 0 " & _
    19.              "WHERE (Sequence IN " & _
    20.              "(SELECT tabEarningRecords.Sequence " & _
    21.              "FROM tabEGPurchase INNER JOIN " & _
    22.              "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
    23.              "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence " & _
    24.              "WHERE tabEGPurchase.Code = " & PurchaseCode & "))"
    25.     If ExecuteSQL(strSQL) = True Then
    26.         MsgBox "Ok!"
    27.     Else
    28.         MsgBox "Not Ok!"
    29.     End If
    30.    
    31.     strSQL = "SELECT tabEGPurchase.Code " & _
    32.              "FROM tabEGPurchase INNER JOIN " & _
    33.              "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
    34.              "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
    35.              "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
    36.              "WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) " & _
    37.              "GROUP BY tabEGPurchase.Code"
    38.     Set adoRecordset2 = New ADODB.Recordset
    39.     With adoRecordset2
    40.         .Open strSQL, adoConn, adOpenForwardOnly, adLockReadOnly
    41.         Do While Not .EOF
    42.             'Get purchase code:
    43.             PurchaseCode = .Fields(0)
    44.            
    45.             'Get total cost:
    46.             strSQL = "FROM tabEGPurchase LEFT OUTER JOIN " & _
    47.                      "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
    48.                      "WHERE tabEGPurchase.Code = " & PurchaseCode & ""
    49.             TotalFrameCost = DesiredField("ISNULL(tabFrameList.BuyingPrice, 0) + ISNULL(tabEGPurchase.MTC, 0)", strSQL)
    50.             MsgBox TotalFrameCost, , "TotalCost"
    51.            
    52.             strSQL = "SELECT  tabEarningRecords.EyeGlass, tabEarningRecords.Expenditure " & _
    53.                      "FROM tabEGPurchase INNER JOIN " & _
    54.                      "tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN " & _
    55.                      "tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN " & _
    56.                      "tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode " & _
    57.                      "WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) AND (tabEGPurchase.Code = " & PurchaseCode & ")"
    58.             Set adoRecordset = New ADODB.Recordset
    59.             Total = TotalFrameCost
    60.            
    61.             With adoRecordset
    62.                 .Open strSQL, adoConn, adOpenForwardOnly, adLockOptimistic
    63.                 Do While Not .EOF
    64.                     curEyeglass = .Fields("Eyeglass")
    65.                     If Total >= curEyeglass Then
    66.                         Total = Total - curEyeglass
    67.                         curExpenditure = curEyeglass
    68.                     Else
    69.                         curExpenditure = Total
    70.                         Total = 0
    71.                     End If
    72.                     .Fields("Expenditure") = curExpenditure
    73.                     .Update
    74.                     strData = "Eyeglass: " & curEyeglass
    75.                     strData = strData & vbNewLine & "Expenditure: " & curExpenditure
    76.                     strData = strData & vbNewLine & "Remaining: " & Total
    77.                     MsgBox strData
    78.                     If Total = 0 Then
    79.                         Exit Do
    80.                     End If
    81.                     .MoveNext
    82.                 Loop
    83.                 .Close
    84.             End With
    85.             .MoveNext
    86.         Loop
    87.         .Close
    88.     End With
    89.     Set adoRecordset = Nothing
    90.     Set adoRecordset2 = Nothing
    91. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Convert VB6.0 code to Stored Procedure

    well...

    It seems the first UPDATE can go into a SPROC pretty much exactyl the way it is.

    The SPROC needs to receive a PURCHASECODE as a parameter - and that goes into the WHERE clause.

    Start the SPROC with BEGIN TRAN and after the UPDATE statement check

    If @@Error<>0
    Begin
    Set @RetText='Not Ok!'
    Rollback
    Return
    End

    Make sure that @RetText is a parameter that is defined for OUTPUT.

    Now the loop - that's going to be much harder.

    I don't understand what the point is of building STRSQL at the top of that other loop - as it's being blown away after the MSGBOX.

    But at any rate - you could use a CURSOR to perform that - it's generally not a good idea to use a cursor but sometimes starting with a CURSOR loop will then make it easier to arrive at a SET BASED solution to complete the job.

    You will have to start the process and post back with what you have for problems - good luck!

    *** 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

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Convert VB6.0 code to Stored Procedure

    Cursor is what I have thought of at first but was hesitant to proceed since there may be a set-based solution that you could offer me first hand. I'll proceed with the CURSOR solution for now and will come back to post it, thanks for the advice!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Convert VB6.0 code to Stored Procedure

    Here is my effort...

    Code:
    IF EXISTS (SELECT name FROM sysobjects 
             WHERE name = 'IncomeCodes' AND type = 'P')
       DROP PROCEDURE IncomeCodes
    GO
    
    CREATE PROCEDURE IncomeCodes AS
    SET NOCOUNT ON
    DECLARE @income_code int, @totalcost money, @eyeglass money, @expenditure money
    DECLARE income_cursor CURSOR FOR 
    	SELECT tabEGPurchase.Code
    	FROM tabEGPurchase INNER JOIN
            tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
            tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN
            tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
    	WHERE (DATEDIFF(Month, tabEarningRecords.DateEarned, GETDATE()) > 0)
    	GROUP BY tabEGPurchase.Code
    
    UPDATE  tabEarningRecords SET Expenditure = 0
    	WHERE (Sequence IN
    		(SELECT tabEarningRecords.Sequence
    			FROM tabEGPurchase INNER JOIN
    			tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
    			tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence))
    	
    OPEN income_cursor
    
    FETCH NEXT FROM income_cursor 
    INTO @income_code
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
    	SELECT @totalcost=ISNULL(tabFrameList.BuyingPrice, 0) + ISNULL(tabEGPurchase.MTC, 0) FROM tabEGPurchase LEFT OUTER JOIN
    	tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
    	WHERE tabEGPurchase.Code = @income_code
    
    	DECLARE income2_cursor CURSOR FOR 
    		SELECT  tabEarningRecords.EyeGlass
    		FROM tabEGPurchase INNER JOIN
    		tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
    		tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN
    		tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
    		WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) AND (tabEGPurchase.Code = @income_code)
    
    	   OPEN income2_cursor
    	   FETCH NEXT FROM income2_cursor INTO @eyeglass
    	
    	   WHILE @@FETCH_STATUS = 0
    	   BEGIN		
    		If (@totalcost >= @eyeglass)
    			BEGIN
    				SET @totalcost = @totalcost - @eyeglass
    				SET @expenditure = @eyeglass
    			END
    		Else
    			BEGIN
    				SET @expenditure = @totalcost
    				SET @totalcost = 0
    			END
    		
    		--UPDATE tabEarningRecords SET Expenditure = @expenditure WHERE 
    		UPDATE tabEarningRecords SET Expenditure = @expenditure
    		WHERE CURRENT OF income2_cursor
    
    		PRINT 'eyeglass    : ' + Convert(NVarChar,@eyeglass)
    		PRINT 'expenditure : ' + Convert(NVarChar,@expenditure)
    		PRINT 'remaining   : ' +  Convert(NVarChar,@totalcost)
    		PRINT ''
    		
    		IF (@totalcost = 0) BREAK
    
    		FETCH NEXT FROM income2_cursor INTO @eyeglass	   
    	   END
    
       CLOSE income2_cursor
       DEALLOCATE income2_cursor
       
       -- Get the next author.
       FETCH NEXT FROM income_cursor 
       INTO @income_code
    END
    
    CLOSE income_cursor
    DEALLOCATE income_cursor
    GO
    So anyone could help me out converting it to set-based solution?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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