Question: I have a VBA procedure in Word that checks to see if a QueryDef exists in an Access database. It was working fine until a few days ago, when it started crashing Word.

The issue seems to be this line: `For Each qdf In db.QueryDefs`. This is the line on which Word hangs and then crashes. I don't think it's my code because I have created a very basic test procedure in a new Word document that is just a loop with nothing in it (see below) and created a new Access database and it still crashes.

A connection to the database is successfully made as I get the locked database file. And this code will successfully execute and print to immediate window: `Debug.Print db.QueryDefs.Count`

I also tested this basic procedure with the empty loop in Excel and it crashes there too. I also updated Office (I'm on Office 365, desktop version of Office). Is there something I'm missing?

`Reference to: Microsoft Office 16.0 Access Database Engine Object Library`

```
Option Explicit

Sub Test()

    Dim qdf As DAO.QueryDef
    Dim db  As DAO.Database

    Set db = OpenDatabase("DatabasePathAndFileName")  

    Debug.Print db.QueryDefs.Count

    For Each qdf In db.QueryDefs

    Next qdf

End Sub

```

Answer: 
Below is one of the hacky way of printing the message every two seconds

We can write our own function and pass that function to Until method of FluentWait. It will call this method every 2 second & Timeout is set to 30 seconds. We are using counter Once count > 10 . It should come out of until . May need some refactoring as per your needs.

```
Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()
 {
    int count = 1;
    public Boolean apply(WebDriver arg0) {
    count++;
    WebElement element = arg0.findElement(By.id("message"));
    String text= element.getText(); 
    System.out.println("Message " + text); // your logic
    if(count > 10))
    {
       return true;
    }
    return false;
    }
 }

```

Example use . Assuming driver paths etc. are set properly

```
public static void main(String[] args) throws InterruptedException {

 WebDriver driver = new FirefoxDriver();
 driver.get("http://localhost:8080");

 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
 wait.pollingEvery(Duration.ofSeconds(2));
 wait.withTimeout(Duration.ofSeconds(30));
 wait.ignoring(NoSuchElementException.class);

 Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()
 {
   int count = 1;
   public Boolean apply(WebDriver arg0) {
   count++;
   WebElement element = arg0.findElement(By.id("message"));
   String text= element.getText(); 
   System.out.println("Message " + text); // your logic
   if(count > 10))
   {
      System.out.println(count + "++++++++++++++++++");
      return true;
   }
   return false;
  }
  };

 wait.until(function);// You can handle timeout exception using try-catch as per your 
 //use case
}

```
Here is another approach:

```java
private boolean testElementFor1SecChange(SearchContext searchContext, By locator, Duration testDuration){
    String baseElementText = searchContext.findElement(locator).getText();
    for(int i = 0; i < testDuration.get(ChronoUnit.SECONDS); i++){
        try {
            Thread.sleep(1000);
            String nextElementText = searchContext.findElement(locator).getText();
            if(baseElementText == null || baseElementText.equals(nextElementText)){
                return false;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}

```

So the test would look like:

```java
@Test
public void testChanges(){
    driver.get("http://localhost:8080");
    Assert.assertTrue(
            testElementFor1SecChange(driver, By.id("message"), Duration.ofSeconds(30))
    );
}

```