Results 1 to 12 of 12
  1. #1
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73

    Code to detect a computer (system) using vba


    Are you able to use ms access vba to code access to automatically detect a computer for example *c:users\(name of the computer eg. ERICK\PDF EXPORTS\FILE NAME??? I don't want to write manually the name of the computer when exporting to pdf

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    i store all client PCs that I export data using their I.P. address.
    ex:
    bob smith, 10.15.5.6, acctg

    Code:
    vTarg = "\\" & txtIP & "\c$\temp\MyFile.xls"
    vSrc = "c:\apps\MyFile.xls"
    
    filecopy vSrc , vTarg

  3. #3
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73
    Option Compare Database


    Function FileExist(FileFullPath As String) As Boolean
    Dim value As Boolean
    value = False
    If Dir(FileFullPath) <> "" Then
    value = True
    End If
    FileExist = value
    End Function
    Private Sub cmd_exportPDF_Click()
    Dim fileName As String, fldrPath As String, filePath As String, LValue As String
    Dim answer As Integer
    Dim rst As Recordset

    fileName = "ALL BOYS(MALES)-" 'filename for PDF file*
    fldrPath = "C:\Users\ERICK\Desktop\PDF Exports" 'folder path where pdf file will be saved *

    (Instead of ERICK code to detect the name of the computer automatically)

  4. #4
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73
    I am getting an error!
    Run-time error '76' Path not found

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Here are 2 functions I learned about years ago.


    Code:
    Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    'Obtained from accessd group Bill Mitchell Aug 31, 2000
    
    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    'Obtained from accessd group Bill Mitchell Aug 31, 2000
    Function CompName()
    
        ' returns: machine name, e.g.:
        '   MONORAILPC
        Dim s As String, n As Long
        n = 255
        s = Space(255)
       
        If GetComputerName(s, n) > 0 Then
            CompName = Left(s, n)
            Debug.Print "Computer is: " & CompName
            MsgBox "This computer is : " & CompName
        End If
        
        
    End Function
    
    Function UserName()
        ' returns: user name, e.g.:
        '   William Mitchell
        Dim s As String, n As Long
        n = 255
        s = Space(255)
        If GetUserName(s, n) > 0 Then
            UserName = Left(s, n)
            Debug.Print "User is: " & UserName
            MsgBox "This computer user is : " & UserName
        End If
    End Function
    Good luck.

  6. #6
    wvmitchell is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2020
    Posts
    24
    wow that code was from 20 years ago

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Bill,
    Yes it is. I remember you giving me that code and some related advice. I mentioned this in the other forum.
    I did not see you participating in the forums until just recently.

  8. #8
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73
    I have created a module and has worked perfectly well giving me the computer name and computer username. The problem is calling this function to vba code.
    I want it to fit here
    *C:\users\computer username extracted from the module\desktop\PDF EXPORTS"

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    Concatenate:

    "C:\users\" & UserName() & "\desktop\PDF Exports"

    Might want to disable or remove Debug.Print and MsgBox lines.

    Or use intrinsic Environ() function (some people don't trust it).

    "C:\users\" & Environ("USERNAME") & "\desktop\PDF Exports"
    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.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    See these links for three ways of getting user name & computer name using VBA: Environ/WScript/APIs:
    http://www.mendipdatasystems.co.uk/g...ame/4594424315
    http://www.mendipdatasystems.co.uk/g...ame/4594488772

    Of the 3 methods, I recommend using WScript as its both simple & cannot be spoofed
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73
    June7
    You are genius. That really worked. Be blessed. Thank you so much.

  12. #12
    erickemboi is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2020
    Posts
    73
    Thank you guys for your help. I really appreciate. God bless you all. You have really assisted me. Thanks once again.

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

Similar Threads

  1. Macro or code to spread DSN into every computer
    By jaryszek in forum Programming
    Replies: 4
    Last Post: 03-27-2018, 02:18 AM
  2. Replies: 5
    Last Post: 09-09-2016, 09:23 AM
  3. Replies: 2
    Last Post: 12-07-2014, 08:33 PM
  4. Replies: 4
    Last Post: 09-18-2012, 08:49 AM
  5. Computer locks on a code error
    By gg80 in forum Programming
    Replies: 6
    Last Post: 05-17-2011, 03:00 AM

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