Coding issues

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Coding issues

Post by bknight »

i have entered some code and it won't compile and I don't understand why

Code: Select all

Function CalcProfit()
'This Function Will Calculate The Profit
Dim db As Database
Dim Rs As Recordset
Dim Fld1 As Field, Fld2 As Field, Fld3 As Field
Dim Fld4 As Field, Fld5 As Field, Fld6 As Field
Dim Fld7 As Field, Fld8 As Field, Fld9 As Field
Dim Fld10 As Field, Fld11 As Field, Fld12 As Field, Fld13 As Field
Dim I As Long, intcurQty As Integer, intnextQty As Integer, intbackRec As Integer
Dim dblNextAmt As Double, dblCurAmt As Double
Dim strCurSymbol As String, strNextSymbol As String, strCurCon As String, strNextCon As String
Set db = CurrentDb
Set Rs = db.OpenRecordset("Select * From Trades Order By Index")
Set Fld1 = Rs!ID
Set Fld2 = Rs!TradeDay
Set Fld3 = Rs!Symbol
Set Fld4 = Rs!Contract
Set Fld5 = Rs!Quantity
Set Fld6 = Rs!ActionID
Set Fld7 = Rs!ActionName
Set Fld8 = Rs!Price
Set Fld9 = Rs!Commission
Set Fld10 = Rs!Fees
Set Fld11 = Rs!Amount
Set Fld12 = Rs!Profit
Set Fld13 = Rs!OrderNum
Rs.MoveFirst
Rs.MoveLast
Rs.MoveFirst
Rs.MoveLast
For I = Rs.RecordCount To 2 Step -1
Rs.Edit
    If Fld6 = 47 Or fld = 49 Then
    'Closing trade find prev opening trades
    strCurSymbol = Fld3
    strCurCon = Fld4
    intcurQty = Fld5
    dblCurAmt = Fld11
        If Fld3 = strCurSymbol And Fld4 = strCurCon Then
        'Found prev opening symbol and contract
             If Fld5 + intcurQty = 0 Then
             'closing quantity equals prev closing quan
             Rs.MoveLast ' move to the closing trade
             dblNextAmt = Fld11
        Else
            intbackRec = Fld5 + intcurQty ' how many quan left
        End If
    End If
'Exit For
Rs.Update
Next I
Rs.Update
Set Rs = Nothing
Set db = Nothing
End Function
Please don't comment on the logic, that is for after the code compiles. I ran this this morning and it failed at Exit For saying Next without a for. I commented that line out as you can see, but the same error continues. The Ifs appear to be correct.

xps35
NewLounger
Posts: 14
Joined: 27 Sep 2022, 07:22
Location: Schiedam, NL

Re: Coding issues

Post by xps35 »

Count the number of If's and the number of End If's...........
Groeten,

Peter

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

That did it, thanks

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

Other remarks:
- You have Rs.Update twice, once within the For ... Next loop, and once outside of it.
- You start editing the record but you don't change the value of any field, so Rs.Update won't do anything.
- CalcProfit is a function but it doesn't return a value (that is not an error in itself, but you don't do anything with the variables that you calculate either)
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

This is a works in progress, there will be problems most anywhere, you have identified one and fixed that. stand by.

Gasman
StarLounger
Posts: 81
Joined: 22 Feb 2022, 09:04

Re: Coding issues

Post by Gasman »

If you indented your code correctly, errors like that would be easier to spot?
Google Smart Indenter if you do not want to do it yourself.
Using Access 2007.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

What would be the proper event to to shift focus from one filed to another? The Microsoft auto-select didn't have any focus choices, Me.??

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

Gasman wrote:
17 Jan 2023, 17:52
If you indented your code correctly, errors like that would be easier to spot?
Google Smart Indenter if you do not want to do it yourself.
Did you look at the entire code? See the indentations?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

The code in the first post in this thread is indented, but not consistently.

If you want to set focus to a control named MyControl, you can use

MyControl.SetFocus
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

As usual I didn't ask the correct question. The control set correctly and highlighted the field, what I wanted was for the cursor to be in the field ready to receive input.
MyControl.???

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

Perhaps

Code: Select all

    With Me.MyControl
        .SetFocus
        .SelStart = 0
        .SelLength = 0
    End With
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

I'll try that in the morning.

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

The cell is highlighted but not "selected, no cursor in the field.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

I can't explain that. I'd have to see the database (again...)
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

You've got it unless you deleted it, pick a field any field and try it. I chose ActioID.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

I don't have the database anymore.
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

You have mail

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

Your code just has SetFocus. The code that I proposed in post 303406 also sets SelStart and SelLength. Any particular reason for ignoring that?

Code: Select all

Private Sub Form_Current()
    With Me.Tradedate
        .SetFocus
        .SelStart = 0
        .SelLength = 0
    End With
End Sub
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Coding issues

Post by bknight »

Here is the current code, not in the works in progress.

Code: Select all

	DoCmd.OpenForm "frmAction", acFormDS
        With Me.ActionID
        .SetFocus
        .SelStart = 0
        .SelLength = 0
        End With

ETA: Added the DoCmd

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Coding issues

Post by HansV »

Me is the form that contains the code, not frmAction.
Did you want to set focus on a control on frmAction?
Best wishes,
Hans