VBA code not working (1 Viewer)

azhar2006

Registered User.
Local time
Today, 12:52
Joined
Feb 8, 2012
Messages
202
Hello all
I try to execute this code, but the message does not appear and confirms that the key exists. Please help.
Thanks >>>AZ
Code:
Private Sub Command2_Click()

Dim MN As Long
Dim DN As Long
Dim EE As Long
Dim APW1 As Variant
Dim UPW2 As Variant

MN = HKEY_CLASSES_ROOT
EE = 1
APW1 = "003"
UPW2 = "003"

DN = QueryValue(MN, "Light\A0\A20\EE", "EE")
If DN = 1 Then
MsgBox "key is present"
End
End If
APV = Me.Text0

CreateNewKey MN, "Light\A0\A20\EE"
CreateNewKey MN, "Light\A0\A20\AP"
CreateNewKey MN, "Light\A0\A20\UP"
SetKeyValue MN, "Light\A0\A20\EE", "EE", EE, 1
SetKeyValue MN, "Light\A0\A20\AP", "APW1", APW1, 1
SetKeyValue MN, "Light\A0\A20\UP", "UPW2", UPW2, 1

End Sub
 
Last edited:

June7

AWF VIP
Local time
Today, 11:52
Joined
Mar 9, 2014
Messages
5,466
Remove that odd "End" line. I tested some code with that line - it compiles but code does not work with it.

QueryValue() appears to be .net function, not VBA.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 20:52
Joined
Sep 21, 2011
Messages
14,237
Seems to be something to with registry?, plus APV is never used? or declared :(
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:52
Joined
Feb 28, 2001
Messages
27,146
According to the link that NG provided, this function only applies to ASP.NET pages. If this isn't running in that context, it won't work. Since Access Web was discontinued some time ago, I would think it would be hard to get into that context unless you had an older version of Access.

Also as correctly noted by June7, your isolated "End" statement that follows the MsgBox statement isn't helping. In fact, I'm rather surprised that it compiled at all, as the "End If" would normally be cut off from its "IF" command, leading to compilation difficulties. I would expect that you should get an "unbalanced If" (or "missing End If") error.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:52
Joined
May 7, 2009
Messages
19,229
is HKEY_CLASSES_ROOT a constant in the expression:

MN = HKEY_CLASSES_ROOT

if not try enclosing HKEY_CLASSES_ROOT in double quote (as string).
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:52
Joined
Sep 21, 2011
Messages
14,237
' Variable = QueryValue(Location, KeyName, ValueName)
' Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE

So why do you have the first parameter as Long? :(
 

isladogs

MVP / VIP
Local time
Today, 20:52
Joined
Jan 14, 2017
Messages
18,209
Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE, , HKEY_USERS

IIRC only the HKCU hive can be edited from Access if it is opened 'normally'
If you need to edit the other hives such as HKLM, this requires Access to be run as an administrator.
 

isladogs

MVP / VIP
Local time
Today, 20:52
Joined
Jan 14, 2017
Messages
18,209
' Variable = QueryValue(Location, KeyName, ValueName)
' Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE

So why do you have the first parameter as Long? :(
This link may explain the reason: RegistryHive Enum (Microsoft.Win32) | Microsoft Docs
Although the quoted values are for a 'foreign machine', the hive values for a local machine are also long integer numbers
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:52
Joined
Sep 21, 2011
Messages
14,237
Yes, OP finally posted some code in a DB that had them as constants. :(
Is that what they call drip feeding?
 

isladogs

MVP / VIP
Local time
Today, 20:52
Joined
Jan 14, 2017
Messages
18,209
I've just opened his app in post #11 though not studied it
This code is from one of my own 'template' databases:

Rich (BB code):
Option Compare Database
Option Explicit

'This module is used to grab registry settings through VBA using WMI

Public Enum Hive
  HKEY_CLASSES_ROOT
  HKEY_CURRENT_USER
  HKEY_LOCAL_MACHINE
  HKEY_USERS
  HKEY_CURRENT_CONFIG
End Enum

'---------------------------------------------------------------------------------------
' Module    : modRegistryNEW
' DateTime  : 26/07/2015
' Authors   : Colin Riddington (except where stated otherwise)
' Website   : http://www.mendipdatasystems.co.uk
' Purpose   : This module is used to grab registry settings through VBA using WMI
' Copyright : The code in theis module MAY be altered and reused in your own applications
' Updated   : Octover 2017
'---------------------------------------------------------------------------------------

'#######################################
Public Function GetHive(hivetype As Hive) As Variant
' return enumerated value depending on the hive chosen
  Select Case hivetype
    Case 0
      GetHive = &H80000000  ' HKEY_CLASSES_ROOT
    Case 1
      GetHive = &H80000001  ' HKEY_CURRENT_USER
    Case 2
      GetHive = &H80000002  ' HKEY_LOCAL_MACHINE
    Case 3
      GetHive = &H80000003  ' HKEY_USERS
    Case 4
      GetHive = &H80000005  ' HKEY_CURRENT_CONFIG
  End Select
End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:52
Joined
May 7, 2009
Messages
19,229
Here the message should appear to me. why do not you show ?
i think Nobody want to get involved, since you are messing with the registry?
wrong code can havoc your OS!
 

azhar2006

Registered User.
Local time
Today, 12:52
Joined
Feb 8, 2012
Messages
202
i think Nobody want to get involved, since you are messing with the registry?
wrong code can havoc your OS!
Just my friend, have a look at the code. Where is the error?
 

June7

AWF VIP
Local time
Today, 11:52
Joined
Mar 9, 2014
Messages
5,466
One error was identified in first response. Did you fix?
 

June7

AWF VIP
Local time
Today, 11:52
Joined
Mar 9, 2014
Messages
5,466
Removing the bad line just allows code to run. Didn't say it would provide result you want.

I did look at file. First I commented the lines that modify registry then ran code. Runs without error. DN is equal to 0 so apparently key is not found. Should it be?

If you are just going to create and set keys anyway, why check if one of them exists?
 

azhar2006

Registered User.
Local time
Today, 12:52
Joined
Feb 8, 2012
Messages
202
Removing the bad line just allows code to run. Didn't say it would provide result you want.

I did look at file. First I commented the lines that modify registry then ran code. Runs without error. DN is equal to 0 so apparently key is not found. Should it be?

If you are just going to create and set keys anyway, why check if one of them exists?
To make sure that the other part of the code in the database containing the tables was accepted by the computer
 

isladogs

MVP / VIP
Local time
Today, 20:52
Joined
Jan 14, 2017
Messages
18,209
I have no problem about editing the registry using my own code ...
Sorry but I'm not prepared to test your code in case it causes problems and I don't have time to spend fixing issues on my PC

Did you understand my comments in post #10?
You are trying to write to the HKEY_CLASSES_ROOT hive (which I really wouldn't recommend).
In order for that to have any chance of working, Access MUST be run as an administrator i.e. with elevated privileges.
Did you do that?

I have an example app which writes to the HKLM hive and therefore also requires Access to be run as an administrator.
JET ShowPlan Manager - Mendip Data Systems (isladogs.co.uk)

Its not the latest version of the app but the registry part of the code is identical.

EDIT:
One quick point. In your code in e.g. post #19, should there be a backslash before \light
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 20:52
Joined
Jan 14, 2017
Messages
18,209
I understand the principle of adding obscure registry keys that would be difficult for anyone to detect / alter.
Even so, I would still advise against using HKCR hive. HKLM would be preferable in my opinion

And despite asking more than once I'm still not clear whether you have tried running the code whilst running Access as an administrator.
If not, nothing will happen.

BTW - do use a different registry key value in the real db just in case one of your end users ever reads this thread ;)
 
Last edited:

azhar2006

Registered User.
Local time
Today, 12:52
Joined
Feb 8, 2012
Messages
202
I understand the principle of adding obscure registry keys that would be difficult for anyone to detect / alter.
Even so, I would still advise against using HKCR hive. HKLM would be preferable in my opinion

And despite asking more than once I'm still not clear whether you have tried running the code whilst running Access as an administrator.
If not, nothing will happen.

BTW - do use a different registry key value in the ral db just in case one of your end users ever reads this thread ;)
BTW - do use a different registry key value in the ral db just in case one of your end users ever reads this thread : OK @isladogs ;) :ROFLMAO:

I will ask UncleThe_Doc_Man to delete the topic permanently hahaha
 

Users who are viewing this thread

Top Bottom