Update data on a closed workbook?

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
I created a form and the values that it takes are Date, Issue, Name, Team Member and Cause.

I use a Production Support Report (workbook that I don't want open for people to see) and it will have tabs for each month ie; January

how can I without opening Production Support Report grab the values from the form then update the Production support report by finding the months sheet and sticking the values at the last row.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
well i found this.. but I don't really understand the SQL part. its excel. what does it use for column headings etc.

Code:
Option Explicit 

 

Sub Update_Records_Worksheet() 

   Dim cnt As ADODB.Connection 

   Dim cmd As ADODB.Command 

   Dim stCon As String, stSQL As String 

   Dim xlCalc As XlCalculation 

 

   'Change settings in order to increase the performance. 

   With Application 

      xlCalc = .Calculation 

      .Calculation = xlCalculationManual 

      .EnableEvents = False 

      .ScreenUpdating = False 

   End With 

 

 

   'Instantiate the ADODB Objects. 

   Set cnt = New ADODB.Connection 

   Set cmd = New ADODB.Command 

 

   'Create the connection string. 

   stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 

                 "Data Source=c:\Source.xls;" & _ 

                 "Extended Properties=""Excel 8.0;HDR=YES"";" 

 

   'Create the SQL query to update the records that meet the condition. 

   stSQL = "UPDATE [Report$] SET Result=20 WHERE Department = 'BB'" 

 

   'Open the connection. 

   cnt.Open stCon 

 

   'Execute the command. 

   With cmd 

      .ActiveConnection = cnt 

      .CommandText = stSQL 

      .Execute 

   End With 

 

   'Close connection. 

   cnt.Close 

 

   'Release objects from memory. 

   Set cmd = Nothing 

   Set cnt = Nothing 

 

   'Restore the settings. 

   With Application 

      .Calculation = xlCalc 

      .EnableEvents = True 

      .ScreenUpdating = True 

   End With 

 

End Sub
 
Upvote 0
The code that you posted uses SQL to update the closed workbook Source.xls, make sure you add in a reference to the Microsoft ActiveX DataObjects x.x Library or it will bomb out.

this line:
stSQL = "UPDATE [Report$] SET Result=20 WHERE Department = 'BB'"
Tells it to update the worksheet named Report and set the column named Result to 20 if the Department is BB

So the Source.xls workbook has a page called Report with the following columns
Department , Result

Ill play with the code in a bit and see if I can figure out a simple insert statement to add to the bottom of the list. Have to finish a project first before the boss comes looking for me.
:D
 
Upvote 0
The code that you posted uses SQL to update the closed workbook Source.xls, make sure you add in a reference to the Microsoft ActiveX DataObjects x.x Library or it will bomb out.

this line:
stSQL = "UPDATE [Report$] SET Result=20 WHERE Department = 'BB'"
Tells it to update the worksheet named Report and set the column named Result to 20 if the Department is BB

So the Source.xls workbook has a page called Report with the following columns
Department , Result

Ill play with the code in a bit and see if I can figure out a simple insert statement to add to the bottom of the list. Have to finish a project first before the boss comes looking for me.
:D

thanks

i somewhat get how it works. what i didn't understand was how it knows columns 'Report' and 'Department' exist.
 
Upvote 0
Ok, boss is coming in late so I played with it a bit more. Try this out. Let me know if it works.

Code:
Option Explicit
Sub Update_Records_Worksheet()
   Dim cnt As ADODB.Connection
   Dim cmd As ADODB.Command
   Dim stCon As String, stSQL As String
   Dim xlCalc As XlCalculation
   Dim workbookVar, monthVar, issueVar, nameVar, teammemberVar, causeVar As String
   Dim dateVar As String
   
   workbookVar = "C:\DELETEME.XLS"
   monthVar = "January"
   dateVar = "01/28/2008"
   issueVar = "the issue"
   nameVar = "the name"
   teammemberVar = "Team member 1"
   causeVar = "wasnt me"
   
   'Change settings in order to increase the performance.
   With Application
      xlCalc = .Calculation
      .Calculation = xlCalculationManual
      .EnableEvents = False
      .ScreenUpdating = False
   End With
   
   'Instantiate the ADODB Objects.
   Set cnt = New ADODB.Connection
   Set cmd = New ADODB.Command

   'Create the connection string.
   stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                 "Data Source=" + workbookVar + ";" & _
                 "Extended Properties=""Excel 8.0;HDR=YES"";"
   
   'Create the SQL query to update the records that meet the condition.
   stSQL = "INSERT INTO [" + monthVar + "$]"
   stSQL = stSQL + " ([Date],Issue,Name,[Team Member],Cause) "
   stSQL = stSQL + "VALUES ('" + dateVar + "','" + issueVar + "','" + nameVar + "','" + teammemberVar + "','" + causeVar + "')"

   'Open the connection.
   cnt.Open stCon

   'Execute the command.
   With cmd
      .ActiveConnection = cnt
      .CommandText = stSQL
      .Execute
   End With

   'Close connection.
   cnt.Close
   
   'Release objects from memory.
   Set cmd = Nothing
   Set cnt = Nothing

   'Restore the settings.
   With Application
      .Calculation = xlCalc
      .EnableEvents = True
      .ScreenUpdating = True
   End With
 
End Sub
 
Last edited:
Upvote 0
FWIW, here is a minor variation on the same approach. I late bound so the reference to ADO is not required.

Code:
'UNTESTED
Sub Update_Records_Worksheet()

  Dim objRecordset As Object

  Dim stCon As String, stSQL As String
  Dim workbookVar As String, monthVar As String, issueVar As String
  Dim nameVar As String, teammemberVar As String, causeVar As String
  Dim dateVar As String

  workbookVar = "C:\DELETEME.XLS"
  monthVar = "January"
  dateVar = "01/28/2008"
  issueVar = "the issue"
  nameVar = "the name"
  teammemberVar = "Team member 1"
  causeVar = "wasnt me"

  'Create the connection string.
  stCon = Join$(Array("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=", workbookVar, _
      ";Extended Properties=""Excel 8.0;HDR=YES"";"), vbNullString)

  'Create the SQL query string
  stSQL = "INSERT INTO [" + monthVar + "$]"
  stSQL = stSQL + " ([Date],Issue,Name,[Team Member],Cause) "
  stSQL = stSQL + "VALUES ('" + dateVar + "','" + issueVar + "','" + nameVar + "','" + teammemberVar + "','" + causeVar + "')"

  Set objRecordset = CreateObject("ADODB.Recordset")
  objRecordset.Open stSQL, stCon
  Set objRecordset = Nothing

End Sub
 
Upvote 0
FWIW, here is a minor variation on the same approach. I late bound so the reference to ADO is not required.

Code:
'UNTESTED
Sub Update_Records_Worksheet()

  Dim objRecordset As Object

  Dim stCon As String, stSQL As String
  Dim workbookVar As String, monthVar As String, issueVar As String
  Dim nameVar As String, teammemberVar As String, causeVar As String
  Dim dateVar As String

  workbookVar = "C:\DELETEME.XLS"
  monthVar = "January"
  dateVar = "01/28/2008"
  issueVar = "the issue"
  nameVar = "the name"
  teammemberVar = "Team member 1"
  causeVar = "wasnt me"

  'Create the connection string.
  stCon = Join$(Array("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=", workbookVar, _
      ";Extended Properties=""Excel 8.0;HDR=YES"";"), vbNullString)

  'Create the SQL query string
  stSQL = "INSERT INTO [" + monthVar + "$]"
  stSQL = stSQL + " ([Date],Issue,Name,[Team Member],Cause) "
  stSQL = stSQL + "VALUES ('" + dateVar + "','" + issueVar + "','" + nameVar + "','" + teammemberVar + "','" + causeVar + "')"

  Set objRecordset = CreateObject("ADODB.Recordset")
  objRecordset.Open stSQL, stCon
  Set objRecordset = Nothing

End Sub

Is it possible to modify the SQL query to update a specific cell in the other workbook that is closed? If so, how can I do that if I use the code above?
 
Upvote 0
Hi Fazza,
Thanks for the sample. I did some tests of my own to come up with the following. This time, with early binding...

-----------------------------------------------
The following behavior was noted during my test:

1) Using a named range with update statements appears to be unpredictable as the named range
may be moved or lost. This should pose no problem with select queries.

2) Trying to use a single cell as a recordset fails, with or without headers. I found that in either case I could
reference the cell I am interested in updating, along with the cell below it or above it - in the latter case
"pretending" there is a header cell.

-- Alex
-----------------------------------------------


hirvenhuuli,
Please pay close attention to the comments at the beginning of the subroutine
about setting a reference to the ADO library.

Code:
Sub ChangeDataInSingleCell()
'---------------------------------
'USER: Please make sure you set references to the ADO object Library
'     1. In the Visual Basic window select Tools on the main menu
'     2.  Then from the Tools menu select References...
'     3.  Then click the box for Microsoft ActiveX Data Objects 2.8 or higher
'---------------------------------

Dim objRecordset As ADODB.Recordset
Dim stConn As String
Dim stSQL As String
Dim workbookVar As String
Dim strTarget As String 'Worksheet and cell address - in brackets with sheet name suffixed with a $
Dim newValueVar As Double

'-----
'Note:
'In referencing the sheet and cell to update,
'    we are interested in one cell.  But we "pretend" there is a
'    field header above it - ADO seems to require at least two cells
'-------------------------------------------------------------------
workbookVar = "C:\MyBook.xls"
strTarget = "[Sheet1$E3:E4]"
newValueVar = 2.7182

'Create the connection string.
stConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
workbookVar & ";Extended Properties=Excel 8.0;"
Debug.Print stConn

'Create the SQL query string
stSQL = "SELECT * FROM " & strTarget
Debug.Print stSQL

Set objRecordset = New ADODB.Recordset
Call objRecordset.Open(stSQL, stConn, CursorTypeEnum.adOpenDynamic, _
        LockTypeEnum.adLockOptimistic, CommandTypeEnum.adCmdText)

'Only one record to be updated
    If Not objRecordset.EOF Then
        objRecordset.Fields(0).Value = newValueVar
        objRecordset.Update
    End If

Set objRecordset = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,566
Messages
6,125,593
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top