title: VBA Code Macro to input user name and password in coding so that macro itself logsin to the data base
body: <p>I am very new to VBA Macro and access... I have created a macro that exports data from access to excel file. Now my access database needs a user name and password every time to execute any table or query. So when I run the macro it also asks for the user name and password to execute further, hence I want to input user name and password in the code itself so upon executing/running a macro itself input the username and password. I have code like this below but it does not work... Please help me.......</p>
  <p><a href="https://i.stack.imgur.com/G7Wao.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G7Wao.png" alt="Macro should automatically input the user name and password in this" /></a></p>
  <p>Below is my code: -</p>
  <pre><code>Dim ws As Excel.Worksheet
  Dim xl As Excel.Application
  Dim wbtarget As Workbook
  Dim FinalExp As QueryDef
  Dim rsFinalExp As Recordset
  Dim arrFound As Variant, arrReplace As Variant
  Dim i As Long
  
  Dim Username_List As Variant
  Dim Passoword_List As Variant
  Dim Username As String
  Dim Password As String
  
  'Extract from Access Database
  
  Set FinalExp = CurrentDb.QueryDefs(&quot;FinalExp&quot;)
  
  Username = &quot;QWERT&quot;
  Password = &quot;TREWQ@1&quot;
  
  Set rsFinalExp = FinalExp.OpenRecordset()
  
  Set xl = CreateObject(&quot;Excel.Application&quot;)
  
  
  'Export to Excel
  
  Set wbtarget = xl.Workbooks.Open(&quot;C:DestinationPath.xlsm&quot;)
  Set ws = wbtarget.Worksheets(&quot;Sample File&quot;)
  
  
  ws.Cells(2, 1).CopyFromRecordset rsFinalExp
  
  'Further modification in excel
  
  arrFound = Array(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;)
      arrReplace = Array(&quot;Ok&quot;, &quot;Deal&quot;, &quot;Run&quot;)
      
      With ws
      
          For i = LBound(arrFound) To UBound(arrFound)
              .Range(&quot;B:B&quot;).Replace what:=arrFound(i), Replacement:=arrReplace(i), _
              LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
              SearchFormat:=False, ReplaceFormat:=False
          Next i
          
      End With
  
  
  wbtarget.Close True
  
  End Sub
  </code></pre>