Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Why does Variable + 1 correct to Variable 1?


    Probably a dumb question but it has been bugging me for a while and I couldn't find any answers on google.

    for example the variable will be named "Count"
    If I type "Count + 1" in some languages it will just increase the variable by 1 (assuming its a numeric value) so sometimes I instinctively type that because that is what I want.
    I know I can do Count = Count + 1 to get the function I want.
    The editor (I forget the name of the thing that autocorrects your code) will change it to "Count 1" though and not work? I am confused on what it is trying to accomplish.

    Does anyone know what it is trying to accomplish and maybe why they decided not to do this?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    It does it because it converts the +1 to 1

    In other words it assumes you are entering a positive value.
    If you type

    Fred - 1

    It leaves it alone as it need the - to indicate it is negative.

    It assume Fred is a sub or function and the value (1 or Minus 1) is being passed to it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    To the best of my knowledge you can't do it the way you're trying to in VBA.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    It does it because it converts the +1 to 1

    In other words it assumes you are entering a positive value.
    If you type

    Fred - 1

    It leaves it alone as it need the - to indicate it is negative.

    It assume Fred is a sub or function and the value (1 or Minus 1) is being passed to it.
    Ah that makes sense! It thinks I am trying to pass the 1 through as an argument for "Fred"!

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    The code will not even compile. Guessing you didn't try.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by June7 View Post
    The code will not even compile. Guessing you didn't try.
    I have not had time to try yet no. I just thought I understood what it was trying to do.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    I thought you had to use ++ in those other languages?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Welshgasman View Post
    I thought you had to use ++ in those other languages?
    I recall doing both. Though I did initially try using the ++ when I was writing a for loop for the first time as I was used to doing i++.

  9. #9
    AlexFim is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2023
    Location
    São Paulo (Brazil)
    Posts
    5
    Hello,
    The "++" operator is used in VB.Net, C# and etc, and does not work in VBA.


    I made a function to represent your doubt.


    '-- by Alex Fim
    Function CountNumber()
    Dim i As Integer
    Dim cnt As Integer


    cnt = 0
    For i = 0 To 10
    cnt = cnt + 1
    Debug.Print "Result:" & cnt
    next i


    End Function


    Click image for larger version. 

Name:	count.PNG 
Views:	20 
Size:	8.3 KB 
ID:	49886


    Try this.


    Alex Fim

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    This has to be because Count in the OP is a string variable (or at least not a number variable)? You certainly can write Count = Count + 1 and the editor should not change anything. Whether or not it compiles is another thing, but to automatically drop the + ?? Makes no sense to me. Think about it - how many times have we all written i = i + 1 and never had the + symbol removed? I can only thing there is an auto correct thing going on in the vbe, assuming that's even possible.

    Or perhaps I don't understand what the issue is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,398
    but to automatically drop the + ?? Makes no sense to me.
    I think it is concatenating the 2 values together much the same as

    Count & 1

  12. #12
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    No, it's because it thinks you are trying to pass "Plus 1" to a sub or function called Count or Fred or Bannana.

    It removes the + as it's unnecessary.
    If you put in -1 it will leave it alone as you are signing the value as negative.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    But this is in a code line, yes?
    The editor (I forget the name of the thing that autocorrects your code) will change it to "Count 1"
    Or is that the part I'm missing? But now I have this nagging feeling that I've seen this before and that it was due to complicated quoting that wasn't just right. I think it's time to post the procedure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Minty View Post
    No, it's because it thinks you are trying to pass "Plus 1" to a sub or function called Count or Fred or Bannana.

    It removes the + as it's unnecessary.
    If you put in -1 it will leave it alone as you are signing the value as negative.
    hah I see this thread has gotten out of hand while I was away. There seem to be quite a few different ideas of what is going on but Minty is describing exactly what I was talking about in the original post.
    Then I stated that in another language(that I don't remember) I recalled seeing that you are able to do "Count + 1" to quickly increment the variable Count by 1.
    Then Welsh stated that in other languages you usually type it like "Count++" to quick increment by 1.

    Though Micron does make an interesting point; If Access knows that Count is an integer, and I'm pretty sure it checks what type of variable it is before doing anything with said variable, then why doesn't it allow you to do either of these quick increment methods?

    Code:
    Option Compare Database
    Option Explicit
    Dim Counttest As Integer
    Private Sub Command0_Click()
    Counttest++ 'Access doesn't see this as valid and says "Expected: expression"
    Count 1 'I typed "Count + 1" but Intellisense corrected it
    Debug.Print Counttest
    End Sub
    
    
    Private Sub Form_Load()
    Counttest = 0
    End Sub
    I wonder if there is another reason besides what Minty said.
    After some googling it is called a Unary Increment/Decrement Operator.
    https://codegym.cc/groups/posts/incr...rators-in-java


    VBA does not support it but VB might. It can also appear as '+='.
    https://stackoverflow.com/questions/...t-exist-in-vba

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    for example the variable will be named "Count"
    If I type "Count + 1
    Except this is the way you write it in vba
    Count = Count + 1
    That won't get concatenated or edited, notwithstanding that you should not name a variable with a reserved word (Count). So I guess post 3 got it right first.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 02-09-2023, 02:47 PM
  2. Replies: 3
    Last Post: 08-02-2021, 09:15 AM
  3. Replies: 2
    Last Post: 05-14-2021, 08:39 AM
  4. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums