Results 1 to 5 of 5
  1. #1
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211

    Set Registry Value Negative Number

    I have some utility functions which allow me to save Access form location for a pop-up form when it is closed, and later when opened it will be moved to that location. In Form_Close event I call this method:



    Code:
    Public Function SaveFormPosition(formName As String) As Boolean
    
        Const REG_DWORD = 4
        Dim regKey  As String
        
        If Forms(formName).windowLeft < 0 Then
            Forms(formName).Move 0, Forms(formName).windowTop
        End If
        If Forms(formName).windowTop < 0 Then
            Forms(formName).Move Forms(formName).windowLeft, 0
        End If
        
        If oRegistry Is Nothing Then Call GetObjects
        With oRegistry
            regKey = TempVars!UserSettingsRegPath & formName & "_XPos"
            .REG_ADD .GetRegPath(regKey), .GetSubKey(regKey), Forms(formName).windowLeft, REG_DWORD
            regKey = TempVars!UserSettingsRegPath & formName & "_YPos"
            .REG_ADD .GetRegPath(regKey), .GetSubKey(regKey), Forms(formName).windowTop, REG_DWORD
        End With
    
    End Function
    oRegistry is a class object to handle registry interaction. I was encountering issues with using Windows API to write registry keys, so I'm using REG.EXE to write the key value as follows:
    Code:
    Public Function REG_ADD(ByVal regKey As String, valueName As String, newValue As String, Optional valueType As KeyTypeEnum = KeyTypeEnum.REG_SZ) As Boolean
    
    
        Dim valTypeName As String
        Dim cmd         As String
        Dim result      As Double
        Dim newVal      As Variant
        
        'Shorten root key name if needed.
        regKey = shorten_reg_key_root(regKey)
        
        'Get text name of value type.
        valTypeName = reg_val_type_name(valueType)
        
        'Build command line for REG.EXE.
        cmd = "REG ADD """ & regKey & """ /f /v """ & valueName & """ /t " & valTypeName & " /d " & newValue
        result = Shell(cmd, vbHide)
        
        REG_ADD = (result <> 0)
    
    
    End Function
    I decided I want to move the form a bit higher on the screen, into the ribbon area and save the negative Y position in the registry. So I commented out the second Move command in the first method above, hoping that the negative Y position would be saved in the registry. Unfortunately, when I look in the registry, the value gets saved as 0.

    I put a break point in and polled the value of "cmd", and it looks like:

    Code:
    REG ADD "HKEY_CURRENT_USER\Software\FedConnect\User Settings" /f /v "ImportExportSettings_YPos" /t REG_DWORD /d -1008
    So what am I doing wrong here? The negative number is getting replaced with 0 when it is written to the registry, or else the registry doesn't allow a negative DWORD value.

    Do I instead need to convert the decimal X & Y positions to HEX and play with the first hex digit to get it to go negative? Can't exactly remember how to do that.

    Thanks...

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I am far from being knowledgeable about what you are trying to do, but I did some testing.

    I used a form for testing using your code (but commented out the reg save lines).

    Code:
    Public Function SaveFormPosition(formName As String) As Boolean
    
        Const REG_DWORD = 4
        Dim regKey As String
    
        'print current form windowtop
    10  Debug.Print Forms(formName).WindowTop
    
    20  If Forms(formName).WindowLeft < 0 Then
    30      Forms(formName).Move 0, Forms(formName).WindowTop
    40  End If
    
    50  If Forms(formName).WindowTop < 0 Then
    60      Forms(formName).Move Forms(formName).WindowLeft, 0   '<<-- WindowTop can/will never be less than Zero!!!
    70  End If
    
        '80    If oRegistry Is Nothing Then Call GetObjects
        '90    With oRegistry
        '100        regKey = TempVars!UserSettingsRegPath & formName & "_XPos"
        '110        .REG_ADD .GetRegPath(regKey), .GetSubKey(regKey), Forms(formName).WindowLeft, REG_DWORD
        '120        regKey = TempVars!UserSettingsRegPath & formName & "_YPos"
        '130        .REG_ADD .GetRegPath(regKey), .GetSubKey(regKey), Forms(formName).WindowTop, REG_DWORD
        '140    End With
    End Function
    I moved the form as far to the top as possible.
    When I ran the code, the debug (line 10) printed the Top as -330 .
    Single stepping through the code, at line 50, the form top is negative, so line 60 sets the form top to zero.
    Then, at line 130, when REG_ADD is called, zero is passed for "Forms(formName).WindowTop".

    Because of line 50, you will never be able to have a window top that is negative.


    Does this make sense?

  3. #3
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Thanks for the reply Steve. You are correct that with the original code posted the form top would be at 0. However, if you review my original post, I mentioned that I commented out that line, and even showed the command line to REG.exe, which shows that the form top WAS at -1008 before writing that value to the registry. So it's not an issue of the form top still being at 0, it's an issue of not being able to write the negative DWORD value to the registry. So I still need to sort that bit out. Thanks again...

  4. #4
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    So I figured it out by doing some experimenting with using hex values instead of decimal values. In the immediate window I did this:

    Code:
    ?hex(1008), hex(-1008)
    3F0           FC10
    Knowing those values, I tried this command:
    Code:
    ? shell("REG ADD ""HKEY_CURRENT_USER\Software\FedConnect\User Settings"" /f /v ""ImportExportSettings_YPos"" /t REG_DWORD /d 0x3F0",vbNormalFocus) 
    25316
    Looking in the registry, I saw the expected value:
    0x000003f0 (1008)

    Then I tried this command:
    Code:
    ? shell("REG ADD ""HKEY_CURRENT_USER\Software\FedConnect\User Settings"" /f /v ""ImportExportSettings_YPos"" /t REG_DWORD /d 0xFC10",vbNormalFocus)
    24316
    Looking in the registry, I saw this value:
    0x0000fc10 (64528)

    So it seems you can't write the negative value to the registry, but if you convert the decimal to HEX values prior to writing, it just writes a very large number to the registry value. Knowing this, I just have to adjust that number by 65536 before using it when I read it out of the registry. Trick is knowing how large the value must be before it is subject to being adjusted by 65536. I decided if the WindowTop property (which is in Twips) read from the registry is larger than the Access window height (in Twips), then subtract 65536 from it:
    Code:
    Public Function SetFormPosition(formName As String) As Boolean
    
        Dim myX         As Long
        Dim myY         As Long
        Dim myWidth     As Long
        Dim myHeight    As Long
        
        Dim scrWidth    As Long
        Dim scrHeight   As Long
        Dim tempPos     As String
        Dim regKey      As String
        Dim wd          As WindowDimensionsType
        
        wd = WindowDimensions
        If oRegistry Is Nothing Then Call GetObjects
        With oRegistry
            
            regKey = TempVars!UserSettingsRegPath & formName & "_XPos"
            tempPos = Nz(.REG_QUERY(.GetRegPath(regKey), .GetSubKey(regKey)), 0)
            If IsNumeric(tempPos) Then
                myX = CLng(tempPos)
            End If
            
            regKey = TempVars!UserSettingsRegPath & formName & "_YPos"
            tempPos = Nz(.REG_QUERY(.GetRegPath(regKey), .GetSubKey(regKey)), 0)
            If IsNumeric(tempPos) Then
                myY = CLng(tempPos)
                If myY > wd.WindowHeightTwips Then myY = myY - 65536
            End If
            
        End With
        
        scrWidth = GetSystemMetrics32(0) * TwipsPerPixelX ' scrWidth in twips
        scrHeight = GetSystemMetrics32(1) * TwipsPerPixelY ' height in twips
        
        With Forms(formName)
            
            myWidth = .insideWidth
            If myX < 0 Then
                myX = 0
            ElseIf myX + myWidth + 1000 > scrWidth Then
                myX = scrWidth - myWidth - 1000
            End If
            
            myHeight = .insideHeight
            If myY + myHeight + 4000 > scrHeight Then
                myY = scrHeight - myHeight - 4000
            End If
            
            .Move myX, myY
            
        End With
    
    
    End Function
    So the wd variable and WindowDimensions method is just a utility that returns a type structure with info about the Access window dimensions. I just compared the value from the registry to the Access window height, and if larger, subtract 65536. Works as needed.

    Thanks so much for the help...

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I guess I misunderstood what you wanted. I *thought* I had been able to write a negative decimal number to the registry...but I've slept since then.

    Happy you were able to figure it out.

    Good luck on your project.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using the (000.00) for a negative number
    By keiath in forum Forms
    Replies: 4
    Last Post: 01-10-2015, 04:43 PM
  2. Calculating a negative number
    By bellevue in forum Forms
    Replies: 4
    Last Post: 04-17-2012, 08:18 AM
  3. Replies: 2
    Last Post: 11-22-2011, 11:45 AM
  4. Replies: 2
    Last Post: 06-17-2009, 09:50 PM
  5. Sum Of Positive Number and Negative Number
    By maysamab in forum Reports
    Replies: 1
    Last Post: 10-20-2008, 04:06 PM

Tags for this Thread

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