While initializing the game I use this code for setting the d3d device (The exact parameters are not important)

Code01:
Code:
Set gD3DDevice = Nothing
Set gD3DDevice = gD3D.CreateDevice(AdapterNumber, DeviceType, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dp_p)
Then I use this code to load an object:

Code02:
Code:
Set Mesh = gD3DX.LoadMeshFromX(strMesh, D3DXMESH_MANAGED, gD3DDevice, Nothing, mtrlBuffer, nMaterials)
If I want to change the resolution etc. of the game changing the mode and using code01 I get this error:

Run-time error '-2005530520 (88760868)':
Automation error

And code01 is highlighted.

Without code02 I don't get that error.

What is the problem?