need to convert a sub into a public function
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 211   1
URL.หัวข้อ / URL
need to convert a sub into a public function

มีคนถามไว้ที่ 
http://www.quicktechusa.com/msgboard/wwwboard.pl?read=22781 
 
เนื้อหาของคำถามมีดังนี้ 
 
I found some VB code on anther web site that extracts the user's System Memory Resources information. I converted (changes the label names to my text box names) the VB for my Access 97 application and it works great in the "forms" module as a private Sub. My problem is that I want to use the code as a public function and have the text boxes in the form(s) to display the System Memory Resources info. What needs to be done to the code to allow my form(s) to use the "strings" (sTotalPhys, sAvailPhys, sTotalVirtual, sAvailVirtual, sTotalPageFile, sAvailPageFile, sMemoryLoad, sUser, sSystem, sGDI, ) from the public function?  
 
'Here is the VB code I converted for my Access 97 db...  
Option Compare Database  
Option Explicit  
 
'http://www.thescarms.com/VBasic/memstats.asp  
 
' On 9x systems you can obtain System Resource information by calling the  
' pBGetFreeSystemResources function in Rsrc32.dll. This dll does some  
' thunking and uses Rsrc16.dll. Due to the 16-bit nature of this function,  
' it is not available on the 32-bit NT and Windows 2000 operating systems.  
 
Const SR = 0  
Const GDI = 1  
Const USR = 2  
Const VER_PLATFORM_WIN32s = 0  
Const VER_PLATFORM_WIN32_WINDOWS = 1  
Const VER_PLATFORM_WIN32_NT = 2  
 
Private Type OSVERSIONINFO  
dwOSVersionInfoSize As Long  
dwMajorVersion As Long  
dwMinorVersion As Long  
dwBuildNumber As Long  
dwPlatformId As Long  
szCSDVersion As String * 128  
End Type  
 
Private Type MEMORYSTATUS  
dwLength As Long ' sizeof(MEMORYSTATUS)  
dwMemoryLoad As Long ' percent of memory in use  
dwTotalPhys As Long ' bytes of physical memory  
dwAvailPhys As Long ' free physical memory bytes  
dwTotalPageFile As Long ' bytes of paging file  
dwAvailPageFile As Long ' free bytes of paging file  
dwTotalVirtual As Long ' user bytes of address space  
dwAvailVirtual As Long ' free user bytes  
End Type  
 
'Loads a MEMORYSTATUS structure with information about the current state of the system’s memory.  
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)  
Private Declare Function pBGetFreeSystemResources Lib "rsrc32.dll" Alias "_MyGetFreeSystemResources32@4" (ByVal iResType As Integer) As Integer  
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long  
 
'Private Sub Form_Load()  
Public Function MemoryStatistics()  
 
Dim MS As MEMORYSTATUS  
Dim OSInfo As OSVERSIONINFO  
 
'Here are the strings I (ghudson) created that I want my form(s) to use to populate their text boxes with  
Dim sTotalPhys As String  
Dim sAvailPhys As String  
Dim sTotalVirtual As String  
Dim sAvailVirtual As String  
Dim sTotalPageFile As String  
Dim sAvailPageFile As String  
Dim sMemoryLoad As String  
Dim sUser As String  
Dim sSystem As String  
Dim sGDI As String  
 
MS.dwLength = Len(MS)  
Call GlobalMemoryStatus(MS)  
With MS  
sTotalPhys = Format$(.dwTotalPhys / 1024, "#,###")  
sAvailPhys = Format$(.dwAvailPhys / 1024, "#,###")  
sTotalVirtual = Format$(.dwTotalVirtual / 1024, "#,###")  
sAvailVirtual = Format$(.dwAvailVirtual / 1024, "#,###")  
sTotalPageFile = Format$(.dwTotalPageFile / 1024, "#,###")  
sAvailPageFile = Format$(.dwAvailPageFile / 1024, "#,###")  
sMemoryLoad = Format$(.dwMemoryLoad, "##0.00") & " %"  
End With  
 
' Operating System/Resource Information.  
OSInfo.dwOSVersionInfoSize = Len(OSInfo)  
Call GetVersionEx(OSInfo)  
If OSInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  
sUser = Format(CStr(pBGetFreeSystemResources(USR)), "##") & " % "  
sSystem = Format(CStr(pBGetFreeSystemResources(SR)), "##") & " % "  
sGDI = Format(CStr(pBGetFreeSystemResources(GDI)), "##") & " % "  
Else  
sUser = "N/A"  
sSystem = "N/A"  
sGDI = "N/A"  
End If  
 
End Function  
 
'Thanks in advance for your help!  
 
'GHudson  
 
Here is the link to the web site where I found the code...  
http://www.thescarms.com/VBasic/memstats.asp  
 
Here is the link to the VB programs source code...  
http://www.thescarms.com/Downloads/MemStats.zip  
 
สรุปคำถาม 
 
เขาได้โค้ดนี้มาจากเว็บไซต์ที่กล่าวไว้ข้างบน เป็นโค้ดที่ใช้ใน Visual Basic ที่แสดงหน่วยความจำประเภทต่างๆ ภายในเครื่องคอมฯ เมื่อเขาดัดแปลงมาใช้ใน Form ของ Access 97 สามารถทำได้งานเป็นอย่างดี 
แต่เขาต้องการที่จะนำฟังก์ชันนี้ไปเก็บไว้ที่ Module แทน (Public Function) แล้วให้สามารถเรียกใช้ในหลายๆ ฟอร์มได้ โดยจะทำอย่างไรให้อ้างอิงถึงค่าต่างๆ ของ sTotalPhys, sAvailPhys, sTotalVirtual, sAvailVirtual, sTotalPageFile, sAvailPageFile, sMemoryLoad, sUser, sSystem, sGDI ได้ 
 
 
ผมแนะนำเขาให้ใช้ Array() เข้ามาช่วยครับ ดังนี้ 
 
Public Function MemoryStatistics() 
 
Dim MS As MEMORYSTATUS 
Dim OSInfo As OSVERSIONINFO 
 
'Here are the strings I (ghudson) created that I want my form(s) to use to populate their text boxes with 
Dim sTotalPhys As String 
Dim sAvailPhys As String 
Dim sTotalVirtual As String 
Dim sAvailVirtual As String 
Dim sTotalPageFile As String 
Dim sAvailPageFile As String 
Dim sMemoryLoad As String 
Dim sUser As String 
Dim sSystem As String 
Dim sGDI As String 
 
MS.dwLength = Len(MS) 
Call GlobalMemoryStatus(MS) 
With MS 
sTotalPhys = Format$(.dwTotalPhys / 1024, "#,###") 
sAvailPhys = Format$(.dwAvailPhys / 1024, "#,###") 
sTotalVirtual = Format$(.dwTotalVirtual / 1024, "#,###") 
sAvailVirtual = Format$(.dwAvailVirtual / 1024, "#,###") 
sTotalPageFile = Format$(.dwTotalPageFile / 1024, "#,###") 
sAvailPageFile = Format$(.dwAvailPageFile / 1024, "#,###") 
sMemoryLoad = Format$(.dwMemoryLoad, "##0.00") & " %" 
End With 
 
' Operating System/Resource Information. 
OSInfo.dwOSVersionInfoSize = Len(OSInfo) 
Call GetVersionEx(OSInfo) 
If OSInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then 
sUser = Format(CStr(pBGetFreeSystemResources(USR)), "##") & " % " 
sSystem = Format(CStr(pBGetFreeSystemResources(SR)), "##") & " % " 
sGDI = Format(CStr(pBGetFreeSystemResources(GDI)), "##") & " % " 
Else 
sUser = "N/A" 
sSystem = "N/A" 
sGDI = "N/A" 
End If 
 
MemoryStatistics = Array(sTotalPhys, sAvailPhys, sTotalVirtual, sAvailVirtual, sTotalPageFile, sAvailPageFile, sMemoryLoad) 
 
End Function 
 
เวลาจะเรียกใช้ก็ทำได้ง่ายๆ เช่น 
ต้องการที่จะดูความจำทั้งหมดของเครื่องคอมฯ ก็ใช้ 
 
MemoryStatistics(0) 
 
ต้องการดูว่าหน่วยความจำเหลือเท่าไร ก็ใช้ 
 
MemoryStatistics(1) 
 
อย่างนี้ไปเรื่อยๆ โดยค่าแรกของ Array() จะเริ่มจาก 0 แล้ว 1, 2, 3, ... 
 
 

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R00262
ไม่มีตัวอย่างจริง ได้งัย? http://agserver.kku.ac.th/basiceng/memstat.zip ครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0581s