0

I need to get the number of processor cores available on a computer programmatically from within MS Access. As an example, the computer I work from most frequently has one processor with 6 cores. I want to grab the number '6' through VBA.

Thus far, I have found two ways to find this information through CMD. (1) I can execute the line echo %NUMBER OF PROCESSORS% and the result is 6 (simple and clean, I like it). (2) I have also tried wmic cpu get numberorcores, but the result of that prompt is as follows:

NumberOfCores
6

I intend to pipe the output to and read from the clipboard. The reason I use the clipboard is to avoid creating, reading, and deleting little text files of data. Prompt (2) works, I can successfully pipe the output to the clipboard and read it into a variable in VBA, but it's messy and I would have to parse the result to get the information I need. I would much prefer using prompt (1), but it's not working and the problem seems to be echo. I have tried using shell() and CreateObject(WScript.Shell).Run without success. The strings I have used to try to execute the echo prompt are as follows:

str = "echo %NUMBER OF PROCESSORS% | clip"
str = "cmd ""echo %NUMBER OF PROCESSORS% | clip"""

So, is there a way to successfully send an echo prompt to CMD through VBA and get a result? Alternatively, is there a different way in VBA to get the number of cores?

TIA!

  • 1
    Have a look to the answer of Randy Shuman at this question stackoverflow.com/q/39274324/7599798. The VB.Script solution works also in VBA (just replace `WScript.Echo` with `Debug.Print`) – FunThomas Oct 04 '21 at 12:54
  • Does this answer your question? [Get Total Number of Cores from a computer WITHOUT HyperThreading](https://stackoverflow.com/questions/39274324/get-total-number-of-cores-from-a-computer-without-hyperthreading) – June7 Oct 04 '21 at 17:09
  • @June7 (and FunThomas, but I can only @ one person at a time it seems) It looks like the [Get Total Number of Cores from a computer WITHOUT HyperThreading](https://www.stackoverflow.com/q/39274324/7599798) thread probably has several good approaches that may work for my problem, so thanks for pointing it out! – Lydia Slaughter Oct 06 '21 at 11:58

1 Answers1

2

Why not keep it simple like this:

Dim result As Variant
result = Environ("NUMBER_OF_PROCESSORS")
Debug.Print "Number of processors is " & result

enter image description here

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25