I am using Excel 2019 Home and Business
I have built SQL string what works if I use it trough PGAdmin, but if I execute it trough vbs i get relation error.
This is how I create connection
db.Open "DSN=" & databaseSource & "; Database=" & databaseName & "; Uid=" & databaseUsername & "; Pwd=" & databasePassword
cmd.CommandType = adCmdText
cmd.ActiveConnection = db
this is the SQL string I am trying to execute. Same string works if used trough PGAdmin
strSQL = "INSERT INTO public.kaup (grupp2, nimi, yhik, viit, grupp1, algyhik, suurus, kaal, baasyhik, tootjamaa, taimoot, teema, varvikood, brand, liik, nimetus, tellitav, varviryhm, kollektsioon) VALUES (" & setT("brand") & ", " & Chr(39) & setName & Chr(39) & ", " & setT("unit") & ", " & setT("isRecipe") & ", " & setT("mainGroupNr") & ", " & setT("algyhik") & ", " & setT("size") & ", " & totalWeightStr & ", " & setT("baasyhik") & ", " & setT("origin") & ", " & setT("taimoot") & ", " & setT("season") & ", " & setT("factoryColor") & ", " & setT("categoryNr") & ", " & setT("productTypeNr") & ", " & setT("factoryName") & ", " & setT("orderable") & ", " & setT("colorGroup") & ", " & setT("collection") & ")"
cmd.CommandText = strSQL
On Error GoTo errorHandler
cmd.Execute
String looks like this:
INSERT INTO public.kaup (grupp2, nimi, yhik, viit, grupp1, algyhik, suurus, kaal, baasyhik, tootjamaa, taimoot, teema, varvikood, brand, liik, nimetus, tellitav, varviryhm, kollektsioon) VALUES (186, 'Nord Active Plus Set Safari + Granite Chassis', 'TK', 1, 2, 'tk', 'N/A', 9.5, 'PCE', 'PL', 'ZZZ', '2020', 166, 2, 885, 'Nord Active Plus Set', 1, 15, null)
I assume I need to add some tables in SQL string, but how would I do that in vbs?
The Table andmebaas is located in 3 different schemas.
Also I removed one another problematic column because in Database it has this written in Create table.
If i add k_m_vaba in my code ith will say it has no relation to kaup_kaibemaks
CONSTRAINT kaup_kaibemaks FOREIGN KEY (k_m_vaba)
REFERENCES eesti.kaibemaks (kaibemaks) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
NOT VALID,
How do I add more connections or relations in my SQL string so that when INSERT new row It knows where are these tables located at?
Found solution. before executing query had to set path to one of the other schemas.
Then Database was able to figure out all the other schemas by itself.
SET search_path TO eesti,public; INSERT INTO kaup (...
Related
query_executed = "SELECT p.FBN, p.Car_Number, p.Car_Title, p.Car_Type, p.Owner car_owner, p.open_closed as Car_Status, p.Car_Status as Status, p.Car_Date_Approved, p.date_submitted_for_approval, p.Expires_After as ""Car Expire"", " & _
"p.Site, p.Car_Lines, p.Capex, p.PO_Committed, p.PO_Invoiced, p.Cluster " & _
"FROM cobrarawdata.mcl_carline p " & _
"WHERE " & UserDefinedFilters & _
"AND (p.Capex > 0 OR p.PO_Committed > 0); "
I have that SQL query which is part of an excel vba used for reporting.
I have a request that we log all of the queries being executed. So I am attempting the following:
#If Mac Then
Sql = "INSERT INTO cobrarawdata.sssr_access_logging (username, event_datetime, event_type, query_executed) VALUES('" & LCase(Environ("USER")) & "','" & Format(Now(), "MM/dd/yyyy HH:mm:ss") & "','CAR Balance Query','" & query_executed & "')"
ExecuteSql (Sql)
#Else
Sql = "INSERT INTO cobrarawdata.sssr_access_logging (username, event_datetime, event_type, query_executed) VALUES('" & LCase(Environ("Username")) & "','" & Format(Now(), "MM/dd/yyyy HH:mm:ss") & "','CAR Balance Query','" & query_executed & "')"
ExecuteSql (Sql)
#End If
I am receiving an error though when I try and use the query_executed in the insert query and I haven't been able to get any amount of jockeying with double quotes to change that.
I found a potential work around. In the query the filters all come through with ' in the string. Removing those allows the concatenation, but it does not exactly represent the query. Should be good enough.
Sql = "INSERT INTO cobrarawdata.sssr_access_logging (username, event_datetime, event_type, query_executed) VALUES('" & LCase(Environ("Username")) & "','" & Format(Now(), "MM/dd/yyyy HH:mm:ss") & "','CAR Balance Query','" & Replace(query_executed, "'", "") & "')"
on my excel sheet the user can choose some video clips and arrange in different order to play as a vlc playlist. Unfortunately it can’t be guaranteed that the video file names haven’t any blanks.
To build the vlc playlist I use successfully:
Dim PL
PL = Shell("C:\Program Files\VideoLAN\VLC\VLC.exe " & certainPath & "\Movie6.flv" & " " & certainPath & "\Movie7.flv" & " " & certainPath & "\Movie8.flv ", 1)
'using "\Movie 6.flv" 'doesn't work of course
'using "\'Movie 6.flv'" 'doesn't work aswell
Is there another way to encapsulate file name with blanks?
thankfull for a hint.
Assuming that certainPath folder end with \ ( es. "C:\" ), this should work:
Dim PL
PL = Shell(chr(34) & "c:\Program Files\VideoLAN\VLC\vlc.exe " & chr(34) & chr(34) & certainPath & "Movie 6.flv" & chr(34) & " " & chr(34) & certainPath & "Movie 7.flv" & chr(34))
CHR is a function to returns the character from the ASCII table (the quotes in this case).
I have an issue, have an AutoCAD file with a ton of data links and would like to update only the data links related to a speciffic table.
Simmilar to the functionality of selecting a table with data links, right clicking and selecting Update Table Data Links.
i have the following code:
Private Sub Update_table_data_link(tblRef As AcadTable)
ThisDrawing.SendCommand "DATALINKUPDATE" & vbCr & "U" & vbCr & "K" & vbCr
End Sub
It works but updates all the data links in the drawing (which is a problem) so a perfect solution would either let me get what links are associated to tblRef
and change the line to:
ThisDrawing.SendCommand "DATALINKUPDATE" & vbCr & "U" & vbCr & "D" & vbCr & "datalink_name_from_tblRef" & vbCr
or directly send the command to update the links to tblRef
After much digging around and a lot of help, here is the answer:
Private Sub Update_table_data_link(tblRef As AcadTable)
ThisDrawing.SendCommand "DATALINKUPDATE " & vbCr & "U" & vbCr & Ent2lspEnt(tblRef) & vbCr & vbCr
End Sub
Public Function Ent2lspEnt(entObj As AcadEntity) As String
'Designed to work with SendCommand, which can't pass objects.
'This gets an objects handle and converts it to a string
'of lisp commands that returns an entity name when run in SendCommand.
Dim entHandle As String
entHandle = entObj.Handle
Ent2lspEnt = "(handent " & Chr(34) & entHandle & Chr(34) & ")"
End Function
note that "Update_table_data_link" has a table as input
I have a number of "ad-hoc" stereotypes in a Sparx EA model. That is, I just typed a stereotype name in the element properties, then used the same name in other elements to which it applies ... without using a profiles.
Each of these then appears in Project --> Settings --> UML Types --> Stereotypes.
Eventually, I added Shape Script icons, etc.
However, I created some confusion with one Stereotype name. I used "Tablespace" as a design concept to indicate "group of related database tables". Our database team finds this confusing due to the physical concept of a "tablespace" in Oracle.
So, I want to rename.
If I do this from UML Types --> Stereotypes, all of the existing elements retain the original name (eg. Tablespace) and revert to their Shape Script-less appearance. If I visit an element and change to the new name, the Shape Script, etc. appears.
I'm not keen on finding each element with stereotype and manually applying the new name.
Is it time for me to learn EA Scripting or is there another way?
Your only escape is using the automation (or native DB access). If your stereotype is just used in objects you may iterate over the result of
Repository.SQLQuery("SELECT object_id FROM t_object WHERE stereotype='oldStereo'")
to get all object ids. Then you need to
elem = Repository.GetElementByID(theId)
to retrieve the single elements. Finally you can change the stereotype with
elem.stereotype = "newStereo"
elem.update()
You can also run a direct SQL with
Repository.Execute("UPDATE t_object SET stereotype='new' WHERE stereotype='old'")
Note that the latter uses one of the landmine functions not being supported officially.
Edit: You might also run the last SQL in a native RDBMS client. When using EAP you might need to rename it temporarily to .mdb to pretend it's an MS Access database (which it actually is).
You can use this VBScript to rename stereotypes in EA.
It has been tested on a .eap file, and it is this complicated because of the lack of replace() in MS Access SQL syntax.
Just go the the Scripting view, add a new VBScript and paste this code to your new script. Then modify it to indicate the from and to stereotypes.
option explicit
!INC Local Scripts.EAConstants-VBScript
'
' Script Name: Rename Stereotypes
' Author: Geert Bellekens
' Purpose: Rename stereotypes on all types of elements
' Environment: Tested on .eap file.
' Date: 13/10/2015
'
sub main
renameElementStereotypes "FromStereo", "ToStereo"
renameAttributeStereotypes "FromStereo", "ToStereo"
renameConnectorStereotypes "FromStereo", "ToStereo"
renameOperationStereotypes "FromStereo", "ToStereo"
renameDiagramStereotypes "FromStereo", "ToStereo"
Repository.RefreshModelView(0)
msgbox "Finished renaming stereotypes"
end sub
sub renameElementStereotypes(fromStereo, toStereo)
renameStereotypes "t_object", fromStereo, toStereo
end sub
sub renameAttributeStereotypes(fromStereo, toStereo)
renameStereotypes "t_attribute", fromStereo, toStereo
end sub
sub renameConnectorStereotypes(fromStereo, toStereo)
renameStereotypes "t_connector", fromStereo, toStereo
end sub
sub renameOperationStereotypes(fromStereo, toStereo)
renameStereotypes "t_operation", fromStereo, toStereo
end sub
sub renameDiagramStereotypes(fromStereo, toStereo)
renameStereotypes "t_diagram", fromStereo, toStereo
end sub
sub renameStereotypes (baseTable, fromStereo, toStereo)
dim updateSQL
'first the second part of of t_xref description
updateSQL = "update (" & baseTable & " o inner join t_xref x on o.[ea_guid] = x.[Client]) "&_
" set x.Description = MID( x.Description, 1, INSTR( x.Description, ':" & fromStereo & "') - 1) "&_
" + ':" & toStereo & "' "&_
" + MID(x.Description,INSTR( x.Description, ':" & fromStereo & "') "&_
" + LEN(':" & fromStereo & "'), LEN(x.Description) "&_
" - INSTR( x.Description, ':" & fromStereo & "') "&_
" - LEN(':" & fromStereo & "')+ 1) "&_
" where o.Stereotype = '" & fromStereo & "' "&_
" and x.Name = 'Stereotypes' "&_
" and INSTR( x.Description, ':" & fromStereo & "') > 0 "
Repository.Execute updateSQL
'then the first part of t_xref description
updateSQL = "update (" & baseTable & " o inner join t_xref x on o.[ea_guid] = x.[Client]) "&_
" set x.Description = MID( x.Description, 1, INSTR( x.Description, '=" & fromStereo & "') - 1) "&_
" + '=" & toStereo & "' "&_
" + MID(x.Description,INSTR( x.Description, '=" & fromStereo & "') "&_
" + LEN('=" & fromStereo & "'), LEN(x.Description) "&_
" - INSTR( x.Description, '=" & fromStereo & "') "&_
" - LEN('=" & fromStereo & "')+ 1) "&_
" where o.Stereotype = '" & fromStereo & "' "&_
" and x.Name = 'Stereotypes' "&_
" and INSTR( x.Description, '=" & fromStereo & "') > 0 "
Repository.Execute updateSQL
'then the stereotype itself
updateSQL = " update " & baseTable & " o "&_
" set o.[Stereotype] = '" & toStereo & "' "&_
" where o.Stereotype = '" & fromStereo & "' "
Repository.Execute updateSQL
end sub
main
Here's the script I created, based on #Thomas Killian's answer.
Improvements:
Uses the EAScriptLib Database functions to get the result.
Proper case on Update() method (didn't seem essential, though)
Also sets the StereotypeEx attribute on the Object/Element. This seems to be critical... otherwise, the change is either ignored or new stereotype is simply added to the list of stereotypes for the object
Also, unsure how much it matters, but did not open the components via Project Browser before running script during final testing. This feels like voodoo.
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-Database
function RenameStereotypes()
{
Repository.EnsureOutputVisible( "Script" );
var objectIDs = DBGetFieldValueArrayString( "object_id" /* : String */, "t_object" /* : String */, "stereotype='DBTables'" /* : String */ ); /* : Array */
Session.Output( objectIDs );
for ( var i = 0; i < objectIDs.length; i++ ) {
var theId = objectIDs[i];
var elem = Repository.GetElementByID(theId);
Session.Output( "Id: " + theId + " Name: " + elem.name + " Stereotype: " + elem.stereotype );
elem.stereotype = "DBTables"; /* FIRST VISIBLE STEREOTYPE */
elem.stereotypeEx = "DBTables"; /* CRITICAL - DEEPER LIST OF STEREOTYPES */
elem.Update(); /* CASE CHANGED, NOT SURE IT MATTERS */
elem.Refresh(); /* JUST IN CASE */
}
}
RenameStereotypes();
I have a classic ASP web application that updates a table in a Excel spreadsheet.
I am using the following to connect to the Excel file
Set rows = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath("data.xlsx") & ";" & _
"Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=Yes" & Chr(34) & ";"
That is a sample query that I am executing
query = "UPDATE [Sheet1$] SET [STATUS] = 'unclaimed', [CLAIMER] = '' ,[SDATE] = '"
& DATE & "' WHERE [JUR] = '" & in_jur & "' AND [WC] = " & in_wc
objConnection.Execute query,numRows,adExecuteNoRecords
The above query gets executed in the process.asp page when the user clicks on a button that calls a javascript function that sends the value of in_jur and in_wc like this
function sendForProcessing() {
somejur = //use the document.getElementByID to get selected jur
somewc = //use the document.getElementByID to get selected wc
window.location = process.asp?in_jur=somejur&in_wc=somewc
}
The application updates the sheet correctly for some time (i counted the number of times it works. 1st test: 10 times 2nd test: 16 times it just varies) and then it stops working.
I do not get error message in the objConnection.Execute.
When i print out the numRows it prints 1.
Is there a better way to update the row in Excel?