Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:31:04+00:00 2026-06-09T21:31:04+00:00

I am working on a spreadsheet tool that queries a SQL Server database and

  • 0

I am working on a spreadsheet tool that queries a SQL Server database and populates various sheets with the results. I provide the user with a simple GUI (formatted cells) to enter their database credentials and an Excel form button which tests the connection. When the button is pressed and the connection string is formed correctly, I identify this by changing the color of a status indicator from red to green. I have added a check to the range of cells that hold the credentials using the Worksheet_Change function, which will switch the status from green back to red if any of the cells are altered.

The problem is users are entering some aspect of their connection string, maybe the last field, and then pressing the ‘Test Connection’ button without first pressing enter or navigating away. and actually writing the value to the cell. My ‘Test Connection’ macro (linked to the button) is called first, switching the status indicator to green (assuming correct credentials), but the Worksheet_Change method does not get called until after the button macro has run through. The result is that the status indicator blinks from green and then back to red despite a establishing a database connection successfully.

I have tried things like manually switching the focus away from the current cell. Before calling my ‘TestConnection’ function from the form button. But so far nothing has worked.

Edit: Some Code…

Private Sub Worksheet_Change(ByVal Target As Range)
    Call SetGlobals

    'Check if database criteria has changed
    If Not Intersect(Target, Target.Worksheet.Range(DB_CELL_RANGE)) Is Nothing Then
        Call UpdateDBStatus(1)
    End If

End Sub

'Connect to database using Main sheet credentials
Function TestConnection()

    'Connection vars
    Set cnn = New ADODB.Connection

    'Open the connection.
    On Error GoTo ConnectError
    cnn.Open GetConnectionString()

    'Update dependencies
    'On Error GoTo FilterError
    Call UpdateFilter("select ********", "F", "F")
    Call UpdateFilter("select *******", "E", "E")
    Call UpdateDBStatus(2)

    MsgBox "Connected successfully to '" & DBASE & "' on machine '" & SERVER & "'"
    'Cleanup
    cnn.Close
    Set cnn = Nothing

    Exit Function

ConnectError:
    Call UpdateDBStatus(1)
    MsgBox "Could not establish a connection."
    Exit Function

FilterError:
    MsgBox "Filter Update Failure."
    Exit Function

End Function

'Set the status of the database connection and mark the result
Public Function UpdateDBStatus(Status As Integer)
    If Status = 1 Then
        Sheets("Main").Range(DB_STATUS_CELL).Value = "Not Connected"
        Sheets("Main").Range(DB_STATUS_CELL).Interior.ColorIndex = 3
        DB_STATUS = False
    Else
        Sheets("Main").Range(DB_STATUS_CELL).Value = "Connected"
        Sheets("Main").Range(DB_STATUS_CELL).Interior.ColorIndex = 4
        DB_STATUS = True
    End If
End Function

Basically if someone is currently editing a cell inside of DB_CELL_RANGE and they presses the ‘Test Connection’ button I would like to have Worksheet_Change complete before calling ‘TestConnection’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:31:05+00:00Added an answer on June 9, 2026 at 9:31 pm

    Answer turned out to be a fairly simple boolean flag which I set to True when a successful database connection is established, then false after the next run of Worksheet_Change finishes. From then only check the DB connection when the flag is false. Code is as follows:

    Public flag As Boolean
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not flag Then
            If Not Intersect(Target, Target.Worksheet.Range(DB_CELL_RANGE)) Is Nothing Then
                UpdateDBStatus (1)
            End If
        Else
            flag = False
        End If
    End Sub
    
    'Connect to database using Main sheet credentials
    Sub TestConnection()
    
        'Connection vars
        Set cnn = New ADODB.Connection
    
        'Open the connection.
        On Error GoTo ConnectError
        cnn.Open GetConnectionString()
    
        'Update dependencies
        On Error GoTo FilterError
        Call UpdateFilter("select ********", "F", "F")
        Call UpdateFilter("select *******", "E", "E")
        Call UpdateDBStatus(2)
    
        flag = True
    
        MsgBox "Connected successfully to '" & DBASE & "' on machine '" & SERVER & "'"
        'Cleanup
        cnn.Close
        Set cnn = Nothing
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code that generates an Excel spreadsheet server-side and then downloads
I am working on a Excel Spreadsheet that when a dropdown box value is
I've been working intermittently on an Excel spreadsheet for a customer that does some
I am working on a complex spreadsheet based solution. Occasionally a user will experience
I'm working on an application at the moment that replicates a spreadsheet like table.
Working with some spreadsheet stuff, internally the columns are referred to by a simple
I am working with 2 sheets in a single spreadsheet and I want to
I'm working on a regression-testing tool that will validate a very large number of
I'm working on an application where the end user defines what columns a database
Is it possible to connect to Google Spreadsheet in GAS using SQL-type queries? If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.